Using Javascript Variables with styled-components

前端 未结 3 852
挽巷
挽巷 2020-12-24 13:42

I\'m using styled-components to build my components. All style properties which accept custom values are reused throughout my components (as it should be). With that in mind

相关标签:
3条回答
  • 2020-12-24 14:14

    I eventually figured this out, so here's how you can do it, at least if using React.

    You need to define the variables in one file and export them.

    // Variables.js
    
    export const FONTSIZE_5 = '20px';
    

    Then you need to import those variables into each component file.

    // Button.js
    
    import * as palette from './Variables.js';
    

    Then you can use the variables in your styled-components like this:

    const Button = styled.button`
      font-size: ${palette.FONTSIZE_5};
    `;
    
    0 讨论(0)
  • 2020-12-24 14:20

    Your final solution works for 2 two reasons:

    1. Simply declaring a variable in a file does not attach it to the global scope for the entire app, so other files are unaware of it unless imported in.
    2. 16px is not a valid value. It needed to either be wrapped in quotes to make it a string (like you did with '20px'), or the px needed to be removed.

    I ran into a similar situation, except I needed my variables to be numbers instead of strings, and this also works:

    const CELL_SIZE = 12;
    const ROWS = 7;
    const CELL_GAP = 3;
    
    const BannerGrid = styled.div`
      display: grid;
      grid-template-columns: repeat(auto-fit, ${CELL_SIZE}px);
      grid-template-rows: repeat(${ROWS}, ${CELL_SIZE}px);
      grid-column-gap: ${CELL_GAP}px;
      grid-row-gap: ${CELL_GAP}px;
      grid-auto-flow: column;
    `;
    
    0 讨论(0)
  • 2020-12-24 14:41

    Wrapping a <ThemeProvider> around your application may help:

    https://www.styled-components.com/docs/advanced#theming

    const theme = {
      fontColour: 'purple'
    }
    
    render() {
      return (
        <ThemeProvider theme={theme}>
          <MyApplication />
        </ThemeProvider>
      )
    }
    

    That will give all child styled-components access to the theme like so:

    const MyApplication = styled.section`
      color: ${props => props.theme.fontColour}
    `
    

    Or

    const MyFancyButton = styled.button`
      background: ${props => props.theme.fontColour}
    `
    

    Or access the theme via https://www.styled-components.com/docs/advanced#getting-the-theme-without-styled-components

    0 讨论(0)
提交回复
热议问题