Socket
Socket
Sign inDemoInstall

jsx-to-styled

Package Overview
Dependencies
82
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    jsx-to-styled

jsx-to-styled adds styled props to your React components


Version published
Weekly downloads
16
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

1.5.3

Readme

Source

JSX to Styled

jsx-to-styled adds styled props to your React components

📝 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

Keywords

FAQs

Last updated on 22 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc