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

Creating Multiple Variants #403

Closed
ghost opened this issue Oct 6, 2019 · 17 comments
Closed

Creating Multiple Variants #403

ghost opened this issue Oct 6, 2019 · 17 comments

Comments

@ghost
Copy link

ghost commented Oct 6, 2019

I am looking for a way to create multiple variants for buttons. For example, let's say I want to have the following three variants:

  1. Size (small, medium, large, etc).
  2. Look (primary, secondary, disabled, etc).
  3. Kind (default, outline, text, etc).

So that I could style a button like this:

<button
 sx={{
    size="buttons.size.large"
    kind="buttons.kind.outline"
    look="buttons.look.secondary"
 }}
/>

This IS possible with style system -- here is an example:

But I can't figure out how to do with with Theme-UI.

Is this possible with Theme-UI? If so, can how can I do it?

Thanks.

@timkindberg
Copy link

Seems like an array could work, where it would just merge the variants.

sx={{
  variant: [
    'buttons.size.large',
    'buttons.kind.outline',
    'buttons.look.secondary'
  ]
}}

@jxnblk
Copy link
Member

jxnblk commented Oct 8, 2019

Currently the variant property only accepts a single argument, but it's something I've considered. @timkindberg the array syntax would allow you to use different variants at different breakpoints

@ghost
Copy link
Author

ghost commented Oct 11, 2019

Okay, thanks for letting me know.

@ghost ghost closed this as completed Oct 11, 2019
@jferrettiboke
Copy link

@jxnblk Any solution to this? I have the same need. Could you consider to re-open this issue?

@jferrettiboke
Copy link

jferrettiboke commented Dec 3, 2019

I get it working doing this. However, it would be cooler to support this internally.

import { useResponsiveValue, useBreakpointIndex } from "@theme-ui/match-media";
import get from "lodash.get";
import theme from "../theme";

function variants(...variantBlocks) {
  const variants = useResponsiveValue(variantBlocks);
  let styles = {};
  variants.map(variant => {
    styles = { ...styles, ...get(theme, variant) };
  });
  return styles;
}

function Example() {
  const index = useBreakpointIndex();
  return (
    <Button
      sx={{
        ...variants(
          ["buttons.colors.secondary", "buttons.sizes.xs"],
          ["buttons.colors.primary", "buttons.sizes.lg"]
        )
      }}
    >
      Breakpoint index: {index}
    </Button>
  );
}

@CanRau
Copy link

CanRau commented Dec 24, 2019

How about comma separated strings? @jxnblk

Merry Christmas to y'all 🎅 🎉

@Pirastrino
Copy link

Seems like an array could work, where it would just merge the variants.

sx={{
  variant: [
    'buttons.size.large',
    'buttons.kind.outline',
    'buttons.look.secondary'
  ]
}}

This approach breaks on breakpoints with e.g. breakpoints: ['40em', '52em', '64em'], buttons.size.large is applied at min 40em, buttons.kind.outline at min 52em etc. and only at min 64em are applied all 3 variants 😔.

@lachlanjc
Copy link
Member

I feel like this isn't quite right, but there is the (obvious?) solution of adding a variants prop that accepts an array of variant values (strings, or I guess arrays if you want the behavior described above with breakpoints).

@CanRau
Copy link

CanRau commented Jan 15, 2020 via email

@lachlanjc
Copy link
Member

Yeah, if they conflict, treat it like CSS declarations, the later declarations override the earlier.

@jxnblk
Copy link
Member

jxnblk commented Jan 16, 2020

We've discussed adding support for a variants key which would deep merge multiple variants, but haven't introduced that yet

@lachlanjc
Copy link
Member

I've made a PR (#586) adding support for this to v0.3! I need a bit of technical help finishing the css support but it's 90% ready to go.

@Segger87 Segger87 mentioned this issue Mar 3, 2020
11 tasks
@nandorojo
Copy link

Are there still plans to finish this PR @lachlanjc? Would be awesome. Thanks to everyone for the great work on theme ui!

@lachlanjc
Copy link
Member

@nandorojo I’d love to, unfortunately the surrounding code has changed a bunch while there was discussion & I’ve had a new job/lot going on recently. Not sure if the specific timeline but I’d love to get back to that soon, it’s functionality I’d use every day 🙂

@thetimmyc
Copy link

I would too @lachlanjc ! :)

@mryechkin
Copy link

Hi folks. Has this been given any more consideration? It would be a great feature to have for our use case.

For example, I need to be able to specify that a button is "disabled" while also being a secondary variant. Is there a clean way of doing this currently?

@lachlanjc
Copy link
Member

@mryechkin No work on this, but still very much wanted! We recently had a discussion about what the API should look like, over here: #1226 (comment)

It needs a fresh PR for implementing, contributions extremely welcome!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants