1.5.3 • Published 1 year ago

jsx-to-styled v1.5.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

📝 What it is ?

Inspired by xstyled and styled-system, jsx-to-styled adds styled props to your React styled-components. The main idea of this library is to stay really simple and performant.

All styled props injected by jsx-to-styled are prefixed by $ symbol to prevent forwarding props to html element, check the styled-components transiant props section for more informations.

🔧 Installation

You must have react and styled-components already installed

npm i jsx-to-styled
# or
yarn add jsx-to-styled

💻 Usage

You can use jsx-to-styled without theme (codesandbox example)

import styled from 'styled-components'
import system from 'jsx-to-styled'

// create your styled component with system props
const Box = styled.div(system)

// now you can use all jsx-to-styled props
return (
  <Box $color="white" $backgroundColor="tomato" $p="30px" $borderRadius="50%">
    Hello World!
  </Box>
)

Or use with a theme to use custom values (codesandbox example)

import styled, { ThemeProvider } from 'styled-components'
import system from 'jsx-to-styled'

// create your styled component with system props
const Box = styled.div(system)

// create your theme
const theme = {
  colors: {
    primary: 'white',
    secondary: 'tomato',
  },
  spaces: {
    xl: '30px',
  },
  radii: {
    half: '50%',
  },
}

// now you can use all jsx-to-styled props with theme values
return (
  <ThemeProvider theme={theme}>
    <Box $color="primary" $backgroundColor="secondary" $p="xl" $borderRadius="half">
      Hello World!
    </Box>
  </ThemeProvider>
)

✨ Typescript

Thanks to csstype, jsx-to-styled is fully typed. You will have autocomplete for all possible css values.

import system, { SystemProps } from 'jsx-to-styled'

// don't forget to add SystemProps type to your styled-component definition
const Box = styled.div<SystemProps>(system)

If you want to access to your theme values, you have to redefine "Theme" interface with your custom theme like that:

// theme.d.ts
import 'jsx-to-styled'
import { theme } from './theme'

type MyTheme = typeof theme

declare module 'jsx-to-styled' {
  export interface Theme extends MyTheme {}
}

📱 Responsive (breakpoints)

You can use breakpoints in your styled props (codesandbox example)

// add "breakpoints" key in your theme
const theme = {
  colors: {
    primary: 'white',
    secondary: 'tomato',
  },
  breakpoints: {
    sm: '600px',
  },
}

// flexDirection will be "column" by default and "row" on screens > 600px
return (
  <Box $display="flex" $flexDirection={{ _: 'column', sm: 'row' }}>
    <Box>A</Box>
    <Box>B</Box>
  </Box>
)

🍱 States

You can use states in your styled props (codesandbox example)

// add "states" key in your theme
const theme = {
  colors: {
    primary: 'white',
    secondary: 'tomato',
  },
  states: {
    hover: '&:hover',
  },
}

// color will be "white" by default and "tomato" on hover
return <Box $color={{ _: 'primary', hover: 'secondary' }}>Hello World!</Box>

📕 Props

System is composed by all of props below

Background

JSX PropertyCSS propertyTheme key
$backgroundbackground
$backgroundImagebackground-image
$backgroundSizebackground-size
$backgroundPositionbackground-position
$backgroundRepeatbackground-repeat

Border

JSX PropertyCSS propertyTheme key
$borderborder
$borderWidthborder-widthborderWidths
$borderStyleborder-style
$borderColorborder-colorcolors
$borderRadiusborder-radiusradii
$borderTopborder-top
$borderTopWidthborder-top-widthborderWidths
$borderTopStyleborder-top-style
$borderTopColorborder-top-colorcolors
$borderTopLeftRadiusborder-top-left-radiusradii
$borderTopRightRadiusborder-top-right-radiusradii
$borderRightborder-right
$borderRightWidthborder-right-widthborderWidths
$borderRightStyleborder-right-style
$borderRightColorborder-right-colorcolors
$borderBottomborder-bottom
$borderBottomWidthborder-bottom-widthborderWidths
$borderBottomStyleborder-bottom-style
$borderBottomColorborder-bottom-colorcolors
$borderBottomLeftRadiusborder-bottom-left-radiusradii
$borderBottomRightRadiusborder-bottom-right-radiusradii
$borderLeftborder-left
$borderLeftWidthborder-left-widthborderWidths
$borderLeftStyleborder-left-style
$borderLeftColorborder-left-colorcolors

Color

JSX PropertyCSS propertyTheme key
$backgroundColorbackground-colorcolors
$colorcolorcolors
$opacityopacity

Flex

JSX PropertyCSS propertyTheme key
$flexflex
$alignContentalign-content
$alignItemsalign-items
$alignSelfalign-self
$justifyContentjustify-content
$justifyItemsjustify-items
$justifySelfjustify-self
$flexBasisflex-basis
$flexDirectionflex-direction
$flexGrowflex-grow
$flexShrinkflex-shrink
$flexWrapflex-wrap
$orderorder

Grid

JSX PropertyCSS propertyTheme key
$gapgapspaces
$columnGapcolumn-gapspaces
$rowGaprow-gapspaces
$gridColumngrid-column
$gridRowgrid-row
$gridAreagrid-area
$gridAutoFlowgrid-auto-flow
$gridAutoColumnsgrid-auto-columns
$gridAutoRowsgrid-auto-rows
$gridTemplateColumnsgrid-template-columns
$gridTemplateRowsgrid-template-rows
$gridTemplateAreasgrid-template-areas

Layout

JSX PropertyCSS propertyTheme key
$wwidthsizes
$hheightsizes
$minWmin-widthsizes
$maxWmax-widthsizes
$minHmin-heightsizes
$maxHmax-heightsizes
$displaydisplay
$verticalAlignvertical-align
$overflowoverflow
$overflowXoverflow-x
$overflowYoverflow-y

Position

JSX PropertyCSS propertyTheme key
$positionposition
$zIndexz-index
$topopspaces
$rightrightspaces
$bottombottomspaces
$leftleftspaces

Space

JSX PropertyCSS propertyTheme key
$mmarginspaces
$mtmargin-topspaces
$mrmargin-rightspaces
$mbmargin-bottomspaces
$mlmargin-leftspaces
$mymargin-top, margin-bottomspaces
$mxmargin-right, margin-leftspaces
$ppaddingspaces
$ptpadding-topspaces
$prpadding-rightspaces
$pbpadding-bottomspaces
$plpadding-leftspaces
$pypadding-top, padding-bottomspaces
$pxpadding-right, padding-leftspaces

Typography

JSX PropertyCSS propertyTheme key
$fontFamilyfont-familyfonts
$fontSizefont-sizefontSizes
$fontWeightfont-weightfontWeights
$lineHeightline-heightlineHeights
$letterSpacingline-spacingletterSpacings
$textAligntext-align
$fontStylefont-style
$textDecorationtext-decoration
$textTransformtext-transform

Other

JSX PropertyCSS propertyTheme key
$cusorcursor
$floatfloat
$objectFitobject-fit
$objectPositionobject-position
$transformtransform
$visibilityvisibility
1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

1.0.0-alpha.3

2 years ago

1.0.0-alpha.2

2 years ago

1.0.0-alpha.1

2 years ago

1.0.0-alpha.0

2 years ago