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

Apply css rules based on html data attribute #1793

Closed
mtt-artis opened this issue Oct 24, 2022 · 8 comments
Closed

Apply css rules based on html data attribute #1793

mtt-artis opened this issue Oct 24, 2022 · 8 comments
Labels
question Further information is requested stale

Comments

@mtt-artis
Copy link

mtt-artis commented Oct 24, 2022

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:

const A = ({state}: {state: "active" | "error"}) => (
  <button
    data-state={state}
    className='[data-state="active"]:bg-blue [data-state="error"]:bg-red'
  >
    my button
  </button>
)
@mtt-artis mtt-artis changed the title apply css rules base on html data attribute Apply css rules based on html data attribute Oct 24, 2022
@sudongyuer sudongyuer added the question Further information is requested label Oct 24, 2022
@therealokoro
Copy link
Contributor

Hmmmmmm 🤔, this could be very useful you know. I guess this should fall under custom selectors

@mtt-artis
Copy link
Author

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 = so i replaced it with a /, i wish someone could make a = version
and I removed the quotes as they might interact with the class string

PS: is there a discord community ?

@zyyv
Copy link
Member

zyyv commented Oct 27, 2022

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?

ps: here is UnoCSS discord community. https://discord.gg/xagTXneNHd

@chu121su12
Copy link
Collaborator

The latest tailwind release contains 2 useful variants: aria and data. I'm unsure how this will interact with attributify but they are quite useful

@mtt-artis
Copy link
Author

i do not know how Radix works under the hood but
it applies data-attributes on html element and they are not accessible from the tsx.

moreover, it's easier if you have multiple attributes to check

@mtt-artis
Copy link
Author

@chu121su12 Are these variants already implemented in preset-wind?

@stale
Copy link

stale bot commented Dec 26, 2022

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.

@stale stale bot added the stale label Dec 26, 2022
@sibbng
Copy link
Member

sibbng commented Dec 26, 2022

This feature is added in #1816

@sibbng sibbng closed this as completed Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

6 participants