question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I custom style Grommet components?

See original GitHub issue

In 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
josiahdahlcommented, Apr 25, 2019

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.

You can also find the theme options in the docs in the Theme section https://v2.grommet.io/button#theme

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

const theme = {
  button: {
      border: {
          width: '5px',
          radius: '0',
      },
      padding: {
          vertical: '1rem',
          horizontal: '2rem',
      };
  };

const CustomTheme =  () => (
  <Grommet theme={theme}>
    <Button>I have a border of 5px with no radius</Button>
  </Grommet>
);

// You might have the import the base theme, not sure...
const StandardTheme = () => (
  <Grommet>
    <Button>I'm the standard purple button with a radius</Button>
  </Grommet>
);

You can apply custom styles on a per-component basis by extending a component’s styles with styled-components.

import styled from 'styled-components`;
import { Button } from 'grommet';
const MyStyledButton = styled(Button)`
  font-weight: bold;
  background-color: #BADA55;
`;

const MyComponent = () => (
  <div>
    Press Button <MyStyledButton label="Press Me" />
  </div>
);

Some components accept an as prop - this doesn’t work when extending them with styled-components. See #2699

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.

2reactions
ShimiSuncommented, Jan 25, 2021

@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)

heading: {
    extend: `color: green`
  }

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

<Heading color='red' />`
...
<Heading color='red' />`
...
<Heading color='red' />`
...

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:

<Heading />`
...
<Heading />`
...
<Heading />`
...

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found