{"id":22476989,"url":"https://github.com/felixtellmann/use-styled-system","last_synced_at":"2026-05-01T15:38:41.965Z","repository":{"id":57388433,"uuid":"297128042","full_name":"FelixTellmann/use-styled-system","owner":"FelixTellmann","description":"A custom React Hook to help you implement \"styled system props\" in combination with styled-jsx for your application.","archived":false,"fork":false,"pushed_at":"2020-10-25T15:50:39.000Z","size":822,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T20:46:12.779Z","etag":null,"topics":["hooks","react","styled-jsx","styled-system"],"latest_commit_sha":null,"homepage":"https://github.com/FelixTellmann/use-styled-system","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FelixTellmann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-20T17:29:09.000Z","updated_at":"2020-10-25T15:50:41.000Z","dependencies_parsed_at":"2022-09-11T20:01:05.070Z","dependency_job_id":null,"html_url":"https://github.com/FelixTellmann/use-styled-system","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FelixTellmann%2Fuse-styled-system","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FelixTellmann%2Fuse-styled-system/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FelixTellmann%2Fuse-styled-system/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FelixTellmann%2Fuse-styled-system/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FelixTellmann","download_url":"https://codeload.github.com/FelixTellmann/use-styled-system/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245897213,"owners_count":20690438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","react","styled-jsx","styled-system"],"created_at":"2024-12-06T14:09:13.215Z","updated_at":"2026-05-01T15:38:41.906Z","avatar_url":"https://github.com/FelixTellmann.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-styled-system\n\nA custom [React Hook](https://reactjs.org/docs/hooks-overview.html) to help you implement \"styled system props\" in combination with styled-jsx for your application.\n\n## How it works\n\n`use-styled-system` provides a custom hook to inject all common [styled-system](https://styled-system.com/) props into `\u003cstyle jsx\u003e` tags. Its especially made to work seamless within any NextJs environment. The hook is completely build with Typescript and provides excellent support for all CSS props, including proper scoping.\n\nCurrently `use-styled-system` uses Javascript and  `.matchMedia` media queries and global context for responsive css. The syntax is the same as with styled system. i.e. \n```JSX\n\u003cBox as='div' p={['20px 10px', 60]}\u003e...\u003c/Box\u003e\n```\n translates into the following for matchMedia below the first breakpoint ie. 600px\n ```HTML\n\u003cdiv class=\"jsx-123\"\u003e\n...\n\u003c/div\u003e\n\u003cstyle\u003e\n    .jsx-123 {\n        padding: 20px 10px\n    }\n\u003c/style\u003e\n```\nand automatically changes to the below once the next media query is triggered.\n ```HTML\n\u003cdiv class=\"jsx-123\"\u003e\n...\n\u003c/div\u003e\n\u003cstyle\u003e\n    .jsx-123 {\n        padding: 60px\n    }\n\u003c/style\u003e\n```\n\n## Roadmap\n\nIn next versions the focus will be on adding support for Pseudo Selectors, CSS based Media Queries and autoprefix injection.\n\n## Requirement\n\nTo use `use-styled-system`, you must use  `styled-jsx@3.0.0`  and `react@16.8.0` or greater which includes Hooks (both shipped with Nextjs).\n\n## Installation\n\n```sh\n$ npm i use-styled-system\n```\n\n## Usage\n\nThe usage within a NextJs environment works out of the box. Any other environment needs [react](https://github.com/facebook/react) \u0026 [styled-jsx](https://github.com/vercel/styled-jsx) as a peer dependency. Refer to their docs for more information.\n\n```tsx\n/* import only the Types that you need */\nimport { FC } from 'react';\nimport { Decor, Layout, Space, useStyledSystem } from 'use-styled-system';\n\nexport const Box: FC\u003cSpace \u0026 Layout \u0026 Decor\u003e = (props) =\u003e {\n  \n  const { styleJsx, nonCssProps } = useStyledSystem(props, { Space: true, Layout: true, Decor: true });\n  \n  return \u003c\u003e\n      \u003cdiv {...nonCssProps}\u003e{props.children}\u003c/div\u003e\n      \u003cstyle jsx\u003e{`\n        div {\n          color: red;\n          ${styleJsx}\n        }\n      `}\u003c/style\u003e\n  \u003c/\u003e\n}\n```\n\n`use-styled-system` returns an Abject with two items:\n    1. `styleJSX` which is a string containing all CSS properties and values\n    2. `nonCssProps` which is an Object with the previously added Props, filtered for any Css props.\n\n### Parameters\n\nYou pass `useStyledSystem` the props (or rest of props, using the `...` spread operator) an optional `Config` object. The configuration object may contain the following keys.\n\n| Key               | Description                                                                          | Type            | Default                                                                                       |\n|:------------------|:-------------------------------------------------------------------------------------|:-------------------------| :-----------------------------------------------------------------------------------|\n|`remBase`          | Sets the base size to use rem properties wherever possible (defaults to 10px)        | number          | 10                                                                                            |\n|`fontSizes`        | Sets the fontsizes shortcuts for common sizes. i.e.`fontSize={3}` equals to `20px`   | (number)[]      | [12, 14, 16, 20, 24, 32, 48, 64, 72]                                                          |\n|`spaceSizes`            | Sets the space shortcuts for common sizes. i.e.`marginBottom={3}` equals to `16px`   | (number)[]      | [0, 4, 8, 16, 32, 64, 128, 256, 512]                                                          |\n|`Padding`          | Activates `Padding` properties to be used as Props                                   | boolean         | false                                                                                         |\n|`Margin`           | Activates `Margin` properties to be used as Props                                    | boolean         | false                                                                                         |\n|`Size`             | Activates `Size` properties to be used as Props                                      | boolean         | false                                                                                         |\n|`Space`            | Shortcut to activate `Padding`, `Margin`, and `Size` properties as Props             | boolean         | false                                                                                         |\n|`Position`          | Activates `Position` properties to be used as Props                                   | boolean         | false                                                                                         |\n|`Flex`           | Activates `Flex` properties to be used as Props                                    | boolean         | false                                                                                         |\n|`Grid`             | Activates `Grid` properties to be used as Props                                      | boolean         | false                                                                                         |\n|`Layout`            | Shortcut to activate `Position`, `Flex`, and `Grid` properties as Props             | boolean         | false                                                                                         |\n|`Border`          | Activates `Border` properties to be used as Props                                   | boolean         | false                                                                                         |\n|`Color`           | Activates `Color` properties to be used as Props                                    | boolean         | false                                                                                         |\n|`Typography`             | Activates `Typography` properties to be used as Props                                      | boolean         | false                                                                                         |\n|`Decor`            | Shortcut to activate `Border`, `Color`, and `Typography` properties as Props             | boolean         | false                                                                                         |\n|`Other`             | Activates `Other` properties to be used as Props                                      | boolean         | false                                                                                         |\n|`All`             | Activates *all* of the above  properties to be used as Props. * **Defaults to true if no other property is selected**                                      | boolean         | false*                                                                                         | \n\n### Return object\n\nA `useStyleSystem` object is returned with the following properties.\n\n| Key         | Description                                             |\n| :---------- | :------------------------------------------------------ |\n| `styleJsx     | string of all css properties    |\n| `nonCssProps`  | Props filtered for any non CSS prop  |\n\n## Examples\n\nUsing `style-jsx/css` `.resolve` functionality to create custom elements.\n\n```tsx\nimport { createElement, FC } from 'react';\nimport { CSS, useStyledSystem } from 'use-styled-system';\nimport { css } from 'styled-jsx/css';\n\ntype BoxProps = {\n  as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'div' | 'code' | 'blockquote' | 'a'\n  className?: string\n  onClick?: Function\n}\n\nexport const Text: FC\u003cBoxProps \u0026 CSS\u003e = ({ as = 'p', children, className = '', ...props }) =\u003e {\n  const { styleJsx, nonCssProps } = useStyledSystem(props, { Decor: true, Space: true, Other: true });\n  const { className: cssClass, styles } = css.resolve`${styleJsx}`;\n  return \u003c\u003e\n    {createElement(as, { className: `${cssClass} ${className}`, ...nonCssProps }, children)}\n    {styles}\n  \u003c/\u003e;\n};\n\nexport default Text;\n```\n\nuse with `styled-jsx-plugin-sass` in a Link Component.\n\n```tsx\nimport { FC, MouseEvent } from 'react';\nimport NextLink from 'next/link';\nimport { Decor, Layout, Space, useStyledSystem } from 'use-styled-system';\n\ntype LinkProps = {\n  onClick?: (event: MouseEvent) =\u003e void\n  href: string\n  title: string | JSX.Element\n  target?: string\n  className?: string\n  secondary?: boolean\n  small?: boolean\n  large?: boolean\n};\n\nexport const Link: FC\u003cLinkProps \u0026 Space \u0026 Layout \u0026 Decor\u003e = ({ onClick, className = '', href, target, title, secondary, small, large, children, ...props }) =\u003e {\n  \n  const { styleJsx, nonCssProps } = useStyledSystem(props, { Space: true, Layout: true, Decor: true });\n  const classNames = `link ${secondary ? 'secondary' : ''} ${small ? 'small' : ''} ${large ? 'large' : ''} ${className}`.trim();\n  return \u003c\u003e\n    \u003cNextLink href={href}\u003e\n      \u003ca target={target} className={classNames} onClick={onClick} {...nonCssProps}\u003e{title}\u003c/a\u003e\n    \u003c/NextLink\u003e\n    \n    \u003cstyle jsx\u003e{`\n      .link {\n        cursor: pointer;\n        color: var(--color-link);\n        font-family: inherit;\n        text-decoration: none;\n        transition: color 0.25s, background-color 0.25s;\n\n        \u0026.small {\n          font-size: 1.4rem;\n        }\n\n        \u0026.large {\n          font-size: 1.8rem;\n        }\n        \n        \u0026:hover {\n          color: var(--color-link-hover)\n        }\n\n        ${styleJsx}\n      }\n    `}\u003c/style\u003e\n  \u003c/\u003e;\n};\n\nexport default Link;\n```\n## License\n\n**[MIT](LICENSE)** Licensed","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelixtellmann%2Fuse-styled-system","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelixtellmann%2Fuse-styled-system","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelixtellmann%2Fuse-styled-system/lists"}