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.

Is there an easy way to customize component?

See original GitHub issue

What problem does this feature solve?

In our project we want to customize button component, such as changing its height. I just saw the V1 theming API but it needs much code.

What does the proposed API look like?

Can we have the way to customize a component like below so we can customize a component with less code:

import theme from '@chakra-ui/theme';

const customTheme = theme.customizeComponent('Button', {
  sizes: {
    md: {
      height: '38px',
    }
  }
})

export default customTheme;

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
with-heartcommented, Aug 6, 2020

I keep coming back to this topic because it is definitely frustrating to have to spread so deeply to add basic modifications, but so far I haven’t come up with anything that isn’t more clear than using deepmerge.

Here’s an example: https://codesandbox.io/s/quizzical-bogdan-vh593?file=/src/App.tsx

You can make whatever modifications you want to the entire theme by passing an object that is deeply merged with the theme.

imo off the top of my head there are a few wins we can gain from making our own API for this though (or wrapping deepmerge with our own typed function(s) at least):

  • typing to guide users to the correct object shapes
  • forced separation of modifications for clarity
  • composability
    • define a modification once, apply to multiple components (example: if you wanted to share a variant across multiple components)
    • function modifications could be automatically composed (example: default component variant is a function, and user defines a variant of the same name as a function—run the default function, run the user-defined function, then deep merge the results together)

Just thinking out loud a little bit here, definitely not sure what the best way to proceed is! But deepmerge allows you to achieve a degree of simplicity immediately if you want it.

3reactions
ljosberinncommented, Aug 5, 2020

Your solution would work, but you’d pollute your autocomplete with a second Button and couldn’t just import from chakra directly. And if you rename it, you’d need everyone working on the project to know what your CustomButton or ThemedButton is called and I bet someone still imports from Chakra directly.

Besides, this would only work for components itself I’m afraid. You couldn’t customize e.g. breakpoints there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strategies for building customizable components - MUI
This article reviews several different approaches that a developer might take to customize UI components, as well as the various tradeoffs ...
Read more >
How To Customize React Components with Props
In this tutorial, you'll create custom components by passing props to your component. Props are arguments that you provide to a JSX element....
Read more >
How to customize your third party React components
We talked about three ways to customize your third party React components. Overwriting CSS; Using Wrapper Component; Modifying the source code.
Read more >
Building highly customizable React components - Medium
1. Define boundaries · 2. Simple use case first approach · 3. Basic customization · 4. Renderers and Components · 5. Custom props...
Read more >
The Easiest Way to Style your Material-UI Components
However, if we change the default color theme, this will change the color of all of the buttons. If we want to customize...
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