styled-components / xstyled

A utility-first CSS-in-JS framework built for React. πŸ’…πŸ‘©β€πŸŽ€βš‘οΈ
https://xstyled.dev
MIT License
2.29k stars 106 forks source link

Incomplete Guide for using xstyled.custom.ts for creating custom xstyled utils #422

Open paramsinghvc opened 4 months ago

paramsinghvc commented 4 months ago

πŸ’¬ Questions and Help

Hi, I'm using xstyled for my project and keen on using some utils like centerAlign, clearFix etc to be used as props on x elements as <x.div centerAlign clearfix ....

I'm using it in a TS project and following this guide https://xstyled.dev/docs/adding-new-utilities/ and https://xstyled.dev/docs/typescript/ It says to create the xstyled.config.js but what to do with it I'm not sure. I created it in the project root but it doesn't seem to work. Also, TS is not able to identify the custom prop of centerAlign added. The TS guide only talks about custom theme types.

Below is my xstyled.cutom.js looks like.

import { createCss, system, compose, style } from '@xstyled/styled-components';

export const clearfix = style({
  prop: 'clearfix',
  css: () => ({
    '&::after': {
      display: 'block',
      content: '',
      clear: 'both',
    },
  }),
});

export const foo = style({
  prop: ['foo'],
  css: ['paddingTop', 'paddingBottom'],
})

export const { css, styled, x, createGlobalStyle } = createCss(compose(system, clearfix, foo));

So, a complete guide or a sandbox would be great. Here's a sandbox https://codesandbox.io/p/sandbox/xstyled-v2-playground-forked-q7dy59

Screenshot 2024-07-09 at 13 29 40
paramsinghvc commented 4 months ago

@gregberge