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.

Cannot override global default style (Heading component)

See original GitHub issue

🐛 Bug report

Hello, I want to override global default styles to HTML elements when extending the theme object, so that when I use the as prop, the overridden default styles would be referenced correctly. For example:

// theme
const theme = extendTheme({
  styles: {
    global: {
      h3: {
        fontSize: "50px",
        fontWeight: "300"
      }
    }
  }
});

// usage
<Heading as="h3">Hello world</Heading>

The fontSize and fontWeight styles above are not applied, but when I add color it works fine. I wasn’t sure if this is a bug or intentional implementation detail.

💻 Link to reproduction

JavaScript CodeSandbox repro

🧐 Expected behavior

New fontSize and fontWeight styles to be correctly applied on h3 element when:

  1. Extending theme global styles
  2. Setting as="h3" on a Heading component

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
advenamecommented, May 27, 2022

This is a valid reason and issue. Using <Heading as="h1">Lorem</Heading> should yield a h1 element without any default size prop. Headings should remain consistent throughout a UI and only have different sizes, when explicitly defined, not the other way around… .

I had even to go that far and disable <Heading/> in our project with the following Eslint rule:

rules:{
    'no-restricted-syntax': [
      'error',
      {
        message : "Don't use Chakra's <Heading/> component. It creates an inconsistent UI with different heading sizes. Use <Text as='h1'/> instead.",
        selector: 'ImportDeclaration[source.value="@chakra-ui/react"] > ImportSpecifier[imported.name="Heading"]',
      },
    ],
}

The other alternative would be creating a custom size named h1, but then you’d have to double specify props <Heading as="h1" size="h1>.

Please reconsider this issue.

2reactions
santialbocommented, Feb 5, 2021

If you want to use the Heading component you need to customize the Heading component on the theme.

const theme = extendTheme({
  components: {
    Heading: {
      baseStyle: {
        fontWeight: "300"
      },
      sizes: {
        // default size is md
        xl: {
          fontSize: "50px",
        },
      },
    },
  },
});

If you don’t want to do that, then use the Text component instead

<Text as="h3">Hello world</Text>

See updated codesandbox https://codesandbox.io/s/old-river-uwpql?file=/src/App.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global css file won't override component css - Stack Overflow
Make sure your style tag within component has scoped attribute otherwise it'll override global styles.
Read more >
Global CSS text styles to override default Pro element styles?
Is it possible to set global styles for elements like headings and paragraphs using style.css or another method, which can be overridden in ......
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 >
Advanced Usage - styled-components
Theming, refs, Security, Existing CSS, Tagged Template Literals, Server-Side Rendering and Style Objects.
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