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
Apply css rules based on html data attribute #1793
Comments
Hmmmmmm 🤔, this could be very useful you know. I guess this should fall under custom selectors |
so i made a custom variant in my unocss config import react from '@vitejs/plugin-react';
import Unocss from 'unocss/vite';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
Unocss({
variants: [
(matcher) => {
const [, attribute = '', value = ''] = matcher.match(/^\[([^\/]+)\/?(.*)\]:/) ?? []
if (!attribute) {
return { matcher }
}
const attributeValue = value ? `="${value}"` : value;
return {
matcher: matcher.replace(/^\[.+\]:(.*)$/, '$1'),
selector: (s) => `[${attribute}${attributeValue}]${s}`,
}
}
]
}),
]
}) and use it like this const A = ({state}: {state: "active" | "error"}) => (
<button
data-state={state}
className='[data-state/active]:bg-blue [data-state/error]:bg-red'
>
my button
</button>
) i had some issues if i kept the PS: is there a discord community ? |
const A = ({state}: {state: "active" | "error"}) => (
const className = state === 'active' ? 'bg-blue' : 'bg-red'
<button className='{className}'>
my button
</button>
) Excuse me for not knowing Radix-UI, but wouldn't it be simpler?
|
i do not know how Radix works under the hood but moreover, it's easier if you have multiple attributes to check |
@chu121su12 Are these variants already implemented in preset-wind? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This feature is added in #1816 |
I'm using Radix-UI and i'd like to apply css rules based on html data attribute.
Is there a syntax or a variant to do so ?
something like this:
The text was updated successfully, but these errors were encountered: