Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle additional cases where rules were getting dropped #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,24 @@ <h3 class="font-bold my-1 text-xl">Radio</h3>

<h2 class="my-2 text-3xl">Layout</h2>

<div class="my-4">
<div class="drawer">
<input id="my-drawer" type="checkbox" class="drawer-toggle" />
<div class="drawer-content">
<!-- Page content here -->
<label for="my-drawer" class="btn btn-primary drawer-button">Open drawer</label>
</div>
<div class="drawer-side">
<label for="my-drawer" aria-label="close sidebar" class="drawer-overlay"></label>
<ul class="menu bg-base-200 text-base-content min-h-full w-80 p-4">
<!-- Sidebar content here -->
<li><a>Sidebar Item 1</a></li>
<li><a>Sidebar Item 2</a></li>
</ul>
</div>
</div>
</div>

<div class="my-4">
<h3 class="font-bold my-1 text-xl">Mask</h3>
<img
Expand Down Expand Up @@ -334,11 +352,23 @@ <h3 class="font-bold my-1 text-xl">Steps</h3>
</div>

<div class="my-4">
<h3 class="font-bold my-1 text-xl">Tab</h3>
<div class="tabs">
<a class="tab tab-bordered">Tab 1</a>
<a class="tab tab-bordered tab-active">Tab 2</a>
<a class="tab tab-bordered">Tab 3</a>
<h3 class="font-bold my-1 text-xl">Tabs (links)</h3>
<div class="tabs tabs-bordered">
<a class="tab">Tab 1</a>
<a class="tab tab-active">Tab 2</a>
<a class="tab">Tab 3</a>
</div>
</div>

<div class="my-4">
<h3 class="font-bold my-1 text-xl">Tabs (input)</h3>
<div class="tabs tabs-bordered w-full">
<input type="radio" name="tabs-input-demo" role="tab" class="tab" aria-label="Tab 1" />
<div role="tabpanel" class="tab-content p-10">Tab 1 Content</div>
<input type="radio" name="tabs-input-demo" role="tab" class="tab" aria-label="Tab 2" checked />
<div role="tabpanel" class="tab-content p-10">Tab 2 Content</div>
<input type="radio" name="tabs-input-demo" role="tab" class="tab" aria-label="Tab 3" />
<div role="tabpanel" class="tab-content p-10">Tab 3 Content</div>
</div>
</div>

Expand Down
25 changes: 23 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import postcss, {type Rule, type ChildNode} from 'postcss'
import autoprefixer from 'autoprefixer'
import {parse, type CssInJs} from 'postcss-js'
import {tokenize, type ClassToken} from 'parsel-js'
import {tokenize, type ClassToken, type PseudoClassToken} from 'parsel-js'
import type {Preset, DynamicRule, Preflight} from 'unocss'
import camelCase from 'camelcase'
import colors from 'daisyui/src/theming/index.js'
Expand Down Expand Up @@ -92,6 +92,27 @@ export const presetDaisy = (
? 'modal'
// Skip prefixes
: (tokens[2] as ClassToken).name
} else if (token.type === 'type') {
// default base to what is likely most specific class by finding the last one in the list
// input.tab:checked + .tab-content, :is(.tab-active, [aria-selected="true"]) + .tab-content -> .tab-content
for (var i = tokens.length - 1; i >= 0; i--) {
if (tokens[i]?.type === 'class') {
base = (tokens[i] as ClassToken).name;
break;
}
}

// html:has(.drawer-open.drawer-open) -> .drawer-open
if (base === '' && token.name === 'html') {
var subTokens = tokenize(node.selector)
if(subTokens.length == 2 && subTokens[1] && (subTokens[1] as PseudoClassToken).name === 'has') {
var hasToken = (subTokens[1] as PseudoClassToken);

if(hasToken.argument) {
base = (tokenize(hasToken.argument!)[0] as ClassToken).name;
}
}
}
}

rules.set(base, (rules.get(base) ?? '') + String(node) + '\n')
Expand Down Expand Up @@ -130,7 +151,7 @@ export const presetDaisy = (
},
themes,
)

return {
name: 'unocss-preset-daisy',
preflights,
Expand Down