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

Document full .json for Components for more precise style overriding #1601

Open
jayliu50 opened this issue Mar 25, 2021 · 3 comments
Open

Document full .json for Components for more precise style overriding #1601

jayliu50 opened this issue Mar 25, 2021 · 3 comments
Labels
affects-docs Changes affect documentation, but not only documentation help wanted Extra attention is needed @theme-ui/components

Comments

@jayliu50
Copy link

jayliu50 commented Mar 25, 2021

Is your feature request related to a problem? Please describe.
I spent many hours trying to figure out how to build upon the styling for Components I saw in the documentation. For example, https://theme-ui.com/components/radio . It was non-trivial to replicate what I saw on the docs site.

Describe the solution you'd like
Point to a .json object that will result in the same style shown in the docs. Extending on the radio example,

Describe alternatives you've considered
In the "base" theme, replicate the exact .json object(s) that will result in what I see on the website, so that I can build off of what is there. Another alternative explored below.

Additional context
I recognize that through theme-ui, I'm free to define whatever styles I want from scratch. Actually, I liked what I saw on the docs, but just wanted to change some colors. I tried to do so, but got ugly results because I didn't have the structure just right.

In other words, a naive developer (me) thought that this should be enough

radio: {
    color: 'green',
    bg: 'transparent'
  }

But in reality, we needed to do something like this (which took me a frustratingly long time to find):

radio: {
    color: 'green',
    bg: 'transparent',
    border: 'thin',
    borderRadius: 'circle',
    ...{
      'input:focus ~ &': {
        bg: 'transparent',
        border: 'thick',
      },
      '> path': {
        fill: 'primary',
      },
      'input:checked ~ &': {
        '> path': {
          fill: 'primary',
        },
      },
    },
  }

Much time could have been saved, had the above .json object been provided in the docs, so devs have a clearer idea of what the starting point actually is.

Another alternative is if I could have done this, that would also have been helpful. Is there a way to do this?

radio: {
    ...themeUI.base.theme.forms.radio,
    color: 'green',
    bg: 'transparent'
  }
@hasparus hasparus added affects-docs Changes affect documentation, but not only documentation @theme-ui/components labels Mar 26, 2021
@hasparus
Copy link
Member

I spent many hours trying to figure out how to build upon the styling for Components I saw in the documentation. For example, https://theme-ui.com/components/radio . It was non-trivial to replicate what I saw on the docs site.

I do agree that the docs should link to source code, but I'm a bit unsure if I understand you correctly.

I wouldn't call these component examples. These components are published as @theme-ui/components and reexported from theme-ui. You are importing the Radio and styling it through "radio" variant, right?

@jayliu50
Copy link
Author

Yes, I am

@jayliu50 jayliu50 changed the title Document default .json for Component Examples Document full .json for Components for more precise style overriding Mar 26, 2021
@fcisio
Copy link
Collaborator

fcisio commented Mar 30, 2021

@jayliu50
I'm also not sure I understand your use case, but you shouldn't need a hack like this.
Make sure that radio is nested in forms like so forms.radio. That way, your styling will be merged with the base for the components.

If you are talking about making it easier to style some components, in most cases playing around with the predefined keys (primary, highlight, ...) should suffice. If you need something more custom, there is no real getting around replicating the variant structure.

It should look like this:

Note that I have removed your object spread. All keys are different, so there is no need for it.

radio: {
    color: 'green',
    bg: 'transparent',
    border: 'thin',
    borderRadius: 'circle',
    'input:focus ~ &': {
      bg: 'transparent',
      border: 'thick',
    },
    '> path': {
      fill: 'primary',
    },
    'input:checked ~ &': {
      '> path': {
        fill: 'primary',
      },
    },
  }

For custom components with complex structures, I like to use CSS variables. That way it becomes easier to style when merging if the theme is used as a base for other projects

customComponent: {
  '--color': 'var(--theme-ui-colors-text)',
  '--bg': 'var(--theme-ui-colors-primary)',
  color: 'var(--color)'
  '::after': {
    content: '""',
    bg: 'var(--bg)',
    // ...
  }
}

When merged, becomes:

customComponent: {
  '--color': 'var(--theme-ui-colors-primary)',
  '--bg': 'var(--theme-ui-colors-secondary)',
}

Hope this helps 👋

@hasparus hasparus added the help wanted Extra attention is needed label Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-docs Changes affect documentation, but not only documentation help wanted Extra attention is needed @theme-ui/components
Projects
None yet
Development

No branches or pull requests

3 participants