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.

Guidance needed: How to override the default styling of Heading components (from @theme-ui/components)?

See original GitHub issue

Hi! First off, thanks for this awesome project!

The Issue

I’m trying to set different colours for different heading levels.

https://theme-ui.com/components/heading says:

The Heading component uses theme.text.heading as its default variant style.

But that’s the same theme.text.heading values for all h1, h2, h3 etc. So the only way to set different colours for different heading levels is to use “overrides” in the styles object: https://theme-ui.com/theming#styles

const theme = {
  styles: {
    h1: {
      color: "hotpink"
    }
  }
};

When using the Styled API, this works great, but it does not work as expected when using <Heading> from @theme-ui/components.

Repro

Here’s my minimal repro - I expect both ‘foo’ and ‘bar’ to be displayed in hotpink:

repro

https://codesandbox.io/s/loving-napier-sbdpf?fontsize=14&hidenavigation=1&theme=dark

My Question

Is there a way to make the <Heading> component use my base styles (w/ different colours at different heading levels) without making consumers pass in a ‘variant’ override each time?

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
magicmarkcommented, Jan 7, 2020

Gotcha, thanks!

For those wanting to use the <Heading /> component, perhaps a new prop (‘level’?) could make things easier?

<Heading level={3}>foo</Heading>

Would imply both as="h3" and variant="styles.h3".

Just providing a data point!

Thanks again!

(Feel free to close this issue if it feels like there’s nothing further to discuss - I can just switch to the Styled component in the meantime.)

1reaction
jxnblkcommented, Jan 6, 2020

You can use any of the theme.styles variants in any component, like the Heading component:

<Heading variant='styles.h2' />
Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Ways to Override Material UI Styles | by John Au-Yeung
There are four main methodologies, implemented using pre-built components and hooks, for overriding styling in Material UI:.
Read more >
How to Override CSS Styles - W3docs
You can set individual styles in your global CSS file as !important overrides inline styles set directly on elements. Example of overriding CSS...
Read more >
Global Styling with Material-UI Theme Overrides and Props
Learn how to use global CSS overrides and default props in a theme to customize all instances of a Material-UI component in a...
Read more >
Override Template Elements with Custom CSS
Use the CSS Editor in Experience Builder to add custom CSS that overrides the default template and Theme panel styles. You can also...
Read more >
Overriding styles with the sx prop | Primer React
Our components are designed to cover common usage patterns, but sometimes a component just isn't quite flexible enough to look the way you...
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