How to use ThemeProvider without wrapping entire App?
@geelen
I am really don't know who is best person to address this question, I found greelen name first in the list so addressing you.
I am creating component library for client which I am using as npm link to use it in create react app. But the question is component library project has no App container so I can not wrap it as
<ThemeProvider>
<App />
</ThemeProvider>
So what is the alternate way I can use themeprovider? How can I use as importing to each component and use theme props?
Please help.
What component library tool are you using to develop? (storybook, styleguidist) They usually offer a way to have wrapper components. Let me know, and I can help you out.
Do I really need need storybook or styleguidist? Without container shouldn't I use theme provider?
For now I have ex. colors.js where all colors are defined and I am importing that colors in every component. Is there any advantage using theme provider instead of this approach? And if yes then let me know how can I leverage theme provider without introducing any other third party library?
You don't technically need to use ThemeProvider to use styled-components.
I think you don't understand what I meant. I said I want to apply theme to my styled component.
Ex. Here is the code for my styledLabel component
import styled from 'styled-components';
import * as fonts from '../styles/fonts';
import * as colors from '../styles/colors';
const Label = styled.label`
font-family: ${({ font }) => font};
font-size: ${({ size }) => size};
font-weight: ${({ weight }) => weight};
color: ${({ color }) => color};
`;
colors.js
sidebar: '#1D242D',
primary1: '#696D73',
primary2: '#535354',
primary3: '#929497',
canvas: '#FFFFFF',
highlight: '#4F80FF',
accent1: '#DBDCDF',
accent2: '#CCCCCC',
alarm: '#EE4623',
background: '#EDEEF1'
};
Here if you can notice I have given color from colors.js.
@Andrew1431 @probablyup is the same as wrapping component in ThemeProvider?
@hiteshagja I don't think I understand your use case, unfortunately. That being said, if you aren't able to reliably wrap your entire app with a ThemeProvider, you can alternatively wrap your individual components with it in the implementation.
import React from 'react';
import styled from 'styled-components';
import * as fonts from '../styles/fonts';
import * as colors from '../styles/colors';
const theme = {
font: fonts.Helvetica,
size: fonts.defaultSize,
weight: 'normal',
color: colors.primary1,
};
const Button = styled.button`
font-family: ${p => p.theme.font};
font-size: ${p => p.theme.size};
font-weight: ${p => p.theme.weight};
color: ${p => p.theme.color};
`;
export default function ThemedButton(props) {
return (
<ThemeProvider theme={theme}>
<Button {...props} />
</ThemeProvider>
);
}
For example.
@probablyup can you please read entire thread first because your answers are irrelevant?
@Andrew1431 If no body can answer from styled components team then please close this issue, because I am not getting proper answer.
I did read it and I said in my response I don’t understand your use case but am trying to help anyway.
In case you didn’t know, we are all volunteers working on this project.
Your attitude and sense of entitlement for a perfect solution to your mostly unelucidated use case is both rude and condescending.