How do I custom style Grommet components?
See original GitHub issueIn the docs I see ThemeContext
. Is this the way to style the components or is there a different way?
I’m just clear on how to add custom styles to all components and one single component.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:17 (3 by maintainers)
Top Results From Across the Web
6 Tips for Styling with Grommet - Atomic Spin
Don't declare the entire theme. · Prioritize the Grommet API, not custom CSS or inline styles. · Use the Grommet theme to reference...
Read more >Grommet v2 - Custom styling outside theme - CodeSandbox
Create Sandbox Sign in. Sandbox Info. Grommet v2 - Custom styling outside theme. showing how to customize components outside the theme.
Read more >the top level grommet container
Custom messages for grommet components. Use this property to define messages or a function to get localized messages for any grommet children components....
Read more >Easy And Accessible React UI with Grommet-UI and Styled ...
Grommet UI is a React-based mobile-first accessible component library for ... and styles, using components as low-level styling construct.
Read more >Using Grommet In React Applications - Smashing Magazine
Grommet is a React component library that boasts of responsive and accessible mobile-first code components. It does this through its components ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @chancesmith, I’ve been working through some of these same questions and here’s a short overview of what I’ve found so far. I hope to contribute to the documentation a bit, as I find it lacking as well.
All of the available theme settings are listed here https://github.com/grommet/grommet/blob/master/src/js/themes/index.d.ts (auto-generated). Line 70 is where the component style descriptions start. Whatever you customize is merged with the base styles found here https://github.com/grommet/grommet/blob/master/src/js/themes/base.js using the
generate
function.Once you’ve customized what you want, you simply pass the theme object to the
Grommet
component and the child components should inherit that styling.Example
You can apply custom styles on a per-component basis by extending a component’s styles with
styled-components
.Let me know if that makes sense or if you have any other questions. I’m sure other people have more insight into it, but this is what I’ve found so far.
@chancesmith long overdue response, thank you for your patient.
I think you raised a good design/dev question on what is the best practice for composing a theme; my personal preference is to leverage the theme to minimize internal prop styling/coding. I’ll use prop styling only when it can’t be done more generically with a theme.
Let’s say we have an app with the same Heading color cross the app (green), and there is only one place that requires Heading with a different color (red). In this case, the theme will dictate the common color, (I’ll be using the
extend
styling since Heading doesn’t accept an explicit color object)so the
color
prop won’t need to be explicit for every Heading component instance, it will stay as simple as<Heading />
. The Heading component that requires a different color will be<Heading color='red' />
. I think it is cleaner this way.A little more advanced example, let’s take the use case where there is only one section in the app that uses different Heading color but uses Heading with a different color (red) multiple times, in this case, instead of duplicating
This may be a good candidate to use ThemeContext just for this scope of code that uses the red Heading color, and define the color on the ThemeContext Heading color, it will simplify the multiple Heading components with the explicit
color
prop, as simple as:I’ve created a quick sandbox that demonstrates the ⬆️ above behavior https://codesandbox.io/s/heading-theme-themecontext-lhevm
I think the theme is a super strong tool, and in order to leverage it properly one needs to ask the right questions - so you are doing it right buddy 😃 I hope I covered and answered your questions, I’m thinking of writing a blog on the theme’s best practices or something along those lines, so feel free to ask more questions if needed.