@slimr/styled 2.1.15 → 2.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,32 +1,20 @@
1
- # @slimr/styled
1
+ # 🪶 @slimr/styled [![npm package](https://img.shields.io/npm/v/@slimr/styled.svg?style=flat-square)](https://npmjs.org/package/@slimr/styled)
2
2
 
3
3
  A tiny (~2kb) React css-in-js library inspired by chakra-ui, emotion, and styled-components libs
4
4
 
5
- Pros:
5
+ Demo: [CodeSandbox](https://codesandbox.io/s/64r9px?file=/src/App.tsx)
6
+
7
+ Features:
8
+
9
+ - Easily create React components with styles using familiar syntax
10
+ - Much less bundle size and runtime sluggishness than others
11
+ - Supports declaring css and styled components inside of Components for better code colocating
12
+ - Zx/Css shorthand props like [chakra-ui](https://chakra-ui.com/docs/styled-system/style-props)
13
+ - Will favor inline-style for performance reasons, if non-responsive and non-stateful.
14
+ - Concise responsive CSS syntax Breakpoints shorthand similar to [chakra-ui](https://chakra-ui.com/docs/styled-system/responsive-styles)
15
+ - Import pre-enhanced HTML Elements like `Div` or `A` for profit
6
16
 
7
- - Much less bundle size and runtime sluggishness
8
- - Less is more: less bugs, no breaking changes
9
- - Supports declaring css and styled components inside of Components for better code colocating and NO MORE NEED TO PASS ARGS!
10
- - Styled shortcuts like styled.div
11
- - Zx/Css shorthand props like [chakra-ui](https://chakra-ui.com/docs/styled-system/style-props):
12
- - Most HTMLElements are enhanced and exported as pascal-case (i.e. `import {Div, A, Section, Flex} from '@slimr/styled'`)
13
- - Pass shorthand props or zx props to styled components. This lib will create css classes if complex, passthrough as styles otherwise.
14
- - `m` --> `margin`
15
- - `mx` --> `margin-left` and right
16
- - `py` --> `padding-top` and bottom
17
- - More [here](https://github.com/bdombro/slimr/blob/65bf012086760b7e481a4064f3be8aea6a098b91/packages/css/src/index.ts#L73)!
18
- - CSS Breakpoints shorthand like [chakra-ui](https://chakra-ui.com/docs/styled-system/responsive-styles):
19
-
20
- ```css
21
- margin: [auto, null, inherit];
22
- /* Translates to */
23
- margin: auto;
24
- @media (min-width: 48em) {
25
- margin: inherit;
26
- }
27
- ```
28
-
29
- - Breakpoints are `[30em, 48em, 62em, 80em, 96em]` and can be overridden by setting `css.breakpoints`
17
+ ## Context
30
18
 
31
19
  `@slimr` is a set of slim React (hence '@slimr') libs:
32
20
 
@@ -46,68 +34,81 @@ Pros:
46
34
  npm i @slimr/styled
47
35
  ```
48
36
 
49
- ## Usage
37
+ ## API
50
38
 
51
- Preview below. For full code, see demos
39
+ ### Includes @slimr/css exports
52
40
 
53
- ```tsx
54
- // Create primitive components if you like
55
- const Box = styled.div`
56
- pos: relative;
57
- `
41
+ See [npm](https://www.npmjs.com/package/@slimr/css) for more info.
58
42
 
59
- interface ButtonProps extends Omit<HtmlTagProps['button'], 'id'> {
60
- id: HtmlTagProps['button']['id'] // make required
61
- }
62
- function Button(props: ButtonProps) {
63
- return (
64
- <button
65
- {...props}
66
- onClick={e => {
67
- console.log(`Button ${props.id} clicked`)
68
- props.onClick?.(e)
69
- }}
70
- />
71
- )
72
- }
73
- const ButtonP = styled(Button)`
74
- bg: red;
75
- c: white;
76
- w: [100%, null, inherit];
43
+ ```typescript
44
+ import {addCSs, createClass, css} from '@slimr/styled'
45
+ addCss('.foo { color: purple }')
46
+ c1 = createClass('c: red;')
47
+ c4 = css`c: red;`
48
+ <div className={css`c: red;`} /> // will resolve to 's0' like above
49
+ c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
50
+ ```
51
+
52
+ ### ZX and Stateful CSS Props
53
+
54
+ Props to help style based on CSS state
55
+
56
+ ```typescript
57
+ import {styled} from '@slimr/styled'
58
+ const MyDiv = styled.div`
59
+ _zx={{color: 'blue}} // applies color = blue
60
+ _active={{color: 'blue'}} // applied on :active
61
+ _dark={{color: 'blue'}} // applied when browser prefers dark modes
62
+ _focus={{color: 'blue'}} // applied on :focus
63
+ _focusVisible={{color: 'blue'}} // applied on :focusVisible
64
+ _focusWithin={{color: 'blue'}} // applied on :focusWithin
65
+ _hover={{color: 'blue'}} // applied on :hover
66
+ _light={{color: 'blue'}} // applied on :light
67
+ _target={{color: 'blue'}} // applied on :target
68
+ _visited={{color: 'blue'}} // applied on :visited
77
69
  `
70
+ ```
71
+
72
+ ### Shorthand props
73
+
74
+ Some styles are available as shorthand -- all of them [here](https://github.com/bdombro/slimr/blob/65bf012086760b7e481a4064f3be8aea6a098b91/packages/css/src/index.ts#L73).
75
+
76
+ ```typescript
77
+ import {styled} from '@slimr/styled'
78
+ const MyDiv = styled.div`
79
+ _bgColor="blue"
80
+ _c="green"
81
+ _p={18}
82
+ _pos="absolute"
83
+ _w={100}
84
+ ```
85
+
86
+ ### Responsive Props
87
+
88
+ Specify responsive styles as arrays, similar to [chakra-ui](https://chakra-ui.com/docs/styled-system/responsive-styles)
89
+
90
+ Default breakpoints are `[30em, 48em, 62em, 80em, 96em]` and can be overridden by setting `css.breakpoints`
91
+
92
+ ```typescript
93
+ import {styled} from '@slimr/styled'
94
+ const MyDiv = styled.div`
95
+ _w=[100%, null, 200px] // width = 100% on mobile, tablet. 200px on > tablet
96
+ _zx={{w: ['100%', null, '200px']}} // is equivalent to _w
97
+ ```
98
+
99
+ ### Pre-Enhanced HTML Elements
100
+
101
+ Import pre-enhanced HTML Elements like `Div` or `A` for profit. With a few exceptions, most elements
102
+ accept ANY CSS style as a prop when prefixed by '_'
78
103
 
79
- export function App() {
80
- const on = useOscillator()
81
-
82
- return (
83
- <Box
84
- // enjoy chakra-ui like shorthand syntax
85
- bg={['lightblue', null, 'lightred']}
86
- >
87
- <ButtonP
88
- // use css if you'd like, which gets converted into a css class and attached to this element
89
- css={`
90
- --font-weight: [bold, null, initial];
91
- `}
92
- id="my-button"
93
- // kinda like style but accepts shorthand syntax
94
- _zx={{
95
- textTransform: on ? 'uppercase' : 'inherit',
96
- }}
97
- // Any attr with '_' prefix will be passed to zx
98
- _fontWeight="var(--font-weight)"
99
- // like _zx, but applies only on :hover
100
- _hover={{bg: 'lightblue'}}
101
- // like _zx, but applies only on :active
102
- _active={{bg: 'lightblue'}}
103
- // like _zx, but applies only when browser prefers dark modes
104
- _dark={{bg: 'lightblue'}}
105
- >
106
- Click me!
107
- </ButtonP>
108
- </Box>
109
- )
110
- }
104
+ ```typescript
105
+ import {Div, A} from '@slimr/styled'
106
+ <Div _p=[8, null, 18]> // a div with responsive padding
107
+ <P _fontSize={30} _lineHeight="1rem"> // style props
108
+ <A _active={{ scale: 1.5 }} _hover={{ c: 'green' }}> // stateful styles
109
+ I grow during keypress and am green on hover
110
+ </A>
111
+ </Div>
111
112
  ```
112
113
 
113
114
  ## Comparisons
@@ -119,7 +120,7 @@ export function App() {
119
120
  Pros
120
121
 
121
122
  - More mature, SSR support
122
- - Premade components
123
+ - Lots of premade components
123
124
 
124
125
  Cons
125
126
 
package/cjs/core.d.ts CHANGED
@@ -27,6 +27,9 @@ export interface SCProps extends _Props {
27
27
  _focusWithin?: Zx;
28
28
  /** Like zx prop, but applies only on :hover */
29
29
  _hover?: Zx;
30
+ /** Like zx prop, but applies only when user prefers dark theme */
31
+ _light?: Zx;
32
+ /** Standard style prop */
30
33
  style?: CSSProperties;
31
34
  /** Like zx prop, but applies only on :target */
32
35
  _target?: Zx;
@@ -45,9 +48,9 @@ export type SC<T extends {
45
48
  className?: HTMLAttributes<allowableAny>['className'];
46
49
  }> = FC<T & SCProps>;
47
50
  /**
48
- * A lightweight alternative to styled-components
49
- * @param function - a functional component to be styled; must accept a className prop
50
- * @returns a function that accepts a template string of css returns a decorated functional component
51
+ * The core functionality of the exported `styled` Object.
52
+ *
53
+ * Not intended to be used directly. Instead, use the `styled` object.
51
54
  */
52
55
  export declare function styledBase<C extends FC<allowableAny>>(Component: C): (strings: string | TemplateStringsArray, ...placeHolders: string[]) => SC<Parameters<C>[0]>;
53
56
  export {};
package/cjs/core.js CHANGED
@@ -5,55 +5,10 @@ exports.styledBase = void 0;
5
5
  const css_1 = require("@slimr/css");
6
6
  const util_1 = require("@slimr/util");
7
7
  const react_1 = require("react");
8
- /** Expands the shorthand props of a zx prop into css full */
9
- function expandShorthandProps(zx) {
10
- return Object.entries(zx).reduce((acc, [k, v]) => {
11
- if (k === 'mx') {
12
- acc.marginLeft = v;
13
- acc.marginRight = v;
14
- }
15
- else if (k === 'my') {
16
- acc.marginTop = v;
17
- acc.marginBottom = v;
18
- }
19
- else if (k === 'px') {
20
- acc.paddingLeft = v;
21
- acc.paddingRight = v;
22
- }
23
- else if (k === 'py') {
24
- acc.paddingTop = v;
25
- acc.paddingBottom = v;
26
- }
27
- else if (k in css_1.shorthandPropsMap) {
28
- acc[(0, util_1.toCamelCase)(css_1.shorthandPropsMap[k])] = v;
29
- }
30
- else {
31
- acc[k] = v;
32
- }
33
- return acc;
34
- }, {});
35
- }
36
- /** Converts a zx prop into css string */
37
- function zxToCss(zx) {
38
- return Object.entries(zx)
39
- .map(([k, v]) => {
40
- if (!v)
41
- return '';
42
- k = (0, util_1.toKebabCase)(k);
43
- if (typeof v === 'number')
44
- v = v + 'px';
45
- if (Array.isArray(v)) {
46
- // @ts-expect-error - TS gets confused by the complexity
47
- v = '[' + v.map(v => (typeof v === 'number' ? v + 'px' : v)).join(',') + ']';
48
- }
49
- return k + ':' + v + ';';
50
- })
51
- .join('\n');
52
- }
53
8
  /**
54
- * A lightweight alternative to styled-components
55
- * @param function - a functional component to be styled; must accept a className prop
56
- * @returns a function that accepts a template string of css returns a decorated functional component
9
+ * The core functionality of the exported `styled` Object.
10
+ *
11
+ * Not intended to be used directly. Instead, use the `styled` object.
57
12
  */
58
13
  function styledBase(Component) {
59
14
  return (...cssProps) => {
@@ -62,7 +17,7 @@ function styledBase(Component) {
62
17
  * A functional component that accepts Styled Props
63
18
  */
64
19
  const CStyled = (0, react_1.forwardRef)(function CStyled(props, ref) {
65
- let { _active, _css, _dark, _focus, _focusVisible, _focusWithin, _hover, _target, _visited, _zx = {}, ...rest } = props;
20
+ let { _active, _css, _dark, _focus, _focusVisible, _focusWithin, _hover, _light, _target, _visited, _zx = {}, ...rest } = props;
66
21
  // Pluck out $ prefixed props
67
22
  Object.entries(props).forEach(([k, v]) => {
68
23
  if (k.startsWith('_')) {
@@ -113,6 +68,13 @@ function styledBase(Component) {
113
68
  &:hover {
114
69
  ${zxToCss(_hover)}
115
70
  }
71
+ `;
72
+ }
73
+ if (_light) {
74
+ cssStr += `
75
+ @media (prefers-color-scheme: light) {
76
+ ${zxToCss(_light)}
77
+ }
116
78
  `;
117
79
  }
118
80
  if (_target) {
@@ -149,4 +111,52 @@ function styledBase(Component) {
149
111
  };
150
112
  }
151
113
  exports.styledBase = styledBase;
114
+ /**********************
115
+ * Helper Functions
116
+ **********************/
117
+ /** Expands the shorthand props of a zx prop into css full */
118
+ function expandShorthandProps(zx) {
119
+ return Object.entries(zx).reduce((acc, [k, v]) => {
120
+ if (k === 'mx') {
121
+ acc.marginLeft = v;
122
+ acc.marginRight = v;
123
+ }
124
+ else if (k === 'my') {
125
+ acc.marginTop = v;
126
+ acc.marginBottom = v;
127
+ }
128
+ else if (k === 'px') {
129
+ acc.paddingLeft = v;
130
+ acc.paddingRight = v;
131
+ }
132
+ else if (k === 'py') {
133
+ acc.paddingTop = v;
134
+ acc.paddingBottom = v;
135
+ }
136
+ else if (k in css_1.shorthandPropsMap) {
137
+ acc[(0, util_1.toCamelCase)(css_1.shorthandPropsMap[k])] = v;
138
+ }
139
+ else {
140
+ acc[k] = v;
141
+ }
142
+ return acc;
143
+ }, {});
144
+ }
145
+ /** Converts a zx prop into css string */
146
+ function zxToCss(zx) {
147
+ return Object.entries(zx)
148
+ .map(([k, v]) => {
149
+ if (!v)
150
+ return '';
151
+ k = (0, util_1.toKebabCase)(k);
152
+ if (typeof v === 'number')
153
+ v = v + 'px';
154
+ if (Array.isArray(v)) {
155
+ // @ts-expect-error - TS gets confused by the complexity
156
+ v = '[' + v.map(v => (typeof v === 'number' ? v + 'px' : v)).join(',') + ']';
157
+ }
158
+ return k + ':' + v + ';';
159
+ })
160
+ .join('\n');
161
+ }
152
162
  //# sourceMappingURL=core.js.map
package/cjs/core.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,oCAA4E;AAE5E,sCAAoD;AACpD,iCAAkF;AAyDlF,6DAA6D;AAC7D,SAAS,oBAAoB,CAAC,EAAM;IAClC,OAAO,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE;QAC/C,IAAI,CAAC,KAAK,IAAI,EAAE;YACd,GAAG,CAAC,UAAU,GAAG,CAAC,CAAA;YAClB,GAAG,CAAC,WAAW,GAAG,CAAC,CAAA;SACpB;aAAM,IAAI,CAAC,KAAK,IAAI,EAAE;YACrB,GAAG,CAAC,SAAS,GAAG,CAAC,CAAA;YACjB,GAAG,CAAC,YAAY,GAAG,CAAC,CAAA;SACrB;aAAM,IAAI,CAAC,KAAK,IAAI,EAAE;YACrB,GAAG,CAAC,WAAW,GAAG,CAAC,CAAA;YACnB,GAAG,CAAC,YAAY,GAAG,CAAC,CAAA;SACrB;aAAM,IAAI,CAAC,KAAK,IAAI,EAAE;YACrB,GAAG,CAAC,UAAU,GAAG,CAAC,CAAA;YAClB,GAAG,CAAC,aAAa,GAAG,CAAC,CAAA;SACtB;aAAM,IAAI,CAAC,IAAI,uBAAiB,EAAE;YACjC,GAAG,CAAC,IAAA,kBAAW,EAAC,uBAAiB,CAAC,CAAmC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;SAC7E;aAAM;YACL,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;SACX;QACD,OAAO,GAAG,CAAA;IACZ,CAAC,EAAE,EAAkC,CAAC,CAAA;AACxC,CAAC;AAED,yCAAyC;AACzC,SAAS,OAAO,CAAC,EAAM;IACrB,OAAO,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IAAI,CAAA;QACvC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACpB,wDAAwD;YACxD,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAA;SAC7E;QACD,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;IAC1B,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC"}
package/cjs/core.ts CHANGED
@@ -42,6 +42,9 @@ export interface SCProps extends _Props {
42
42
  _focusWithin?: Zx
43
43
  /** Like zx prop, but applies only on :hover */
44
44
  _hover?: Zx
45
+ /** Like zx prop, but applies only when user prefers dark theme */
46
+ _light?: Zx
47
+ /** Standard style prop */
45
48
  style?: CSSProperties
46
49
  /** Like zx prop, but applies only on :target */
47
50
  _target?: Zx
@@ -59,50 +62,10 @@ export interface SCProps extends _Props {
59
62
  /** Styled Component: Like FunctionalComponent but adds SCProps */
60
63
  export type SC<T extends {className?: HTMLAttributes<allowableAny>['className']}> = FC<T & SCProps>
61
64
 
62
- /** Expands the shorthand props of a zx prop into css full */
63
- function expandShorthandProps(zx: Zx) {
64
- return Object.entries(zx).reduce((acc, [k, v]) => {
65
- if (k === 'mx') {
66
- acc.marginLeft = v
67
- acc.marginRight = v
68
- } else if (k === 'my') {
69
- acc.marginTop = v
70
- acc.marginBottom = v
71
- } else if (k === 'px') {
72
- acc.paddingLeft = v
73
- acc.paddingRight = v
74
- } else if (k === 'py') {
75
- acc.paddingTop = v
76
- acc.paddingBottom = v
77
- } else if (k in shorthandPropsMap) {
78
- acc[toCamelCase(shorthandPropsMap[k as keyof typeof shorthandPropsMap])] = v
79
- } else {
80
- acc[k] = v
81
- }
82
- return acc
83
- }, {} as Record<string, allowableAny>)
84
- }
85
-
86
- /** Converts a zx prop into css string */
87
- function zxToCss(zx: Zx): string {
88
- return Object.entries(zx)
89
- .map(([k, v]) => {
90
- if (!v) return ''
91
- k = toKebabCase(k)
92
- if (typeof v === 'number') v = v + 'px'
93
- if (Array.isArray(v)) {
94
- // @ts-expect-error - TS gets confused by the complexity
95
- v = '[' + v.map(v => (typeof v === 'number' ? v + 'px' : v)).join(',') + ']'
96
- }
97
- return k + ':' + v + ';'
98
- })
99
- .join('\n')
100
- }
101
-
102
65
  /**
103
- * A lightweight alternative to styled-components
104
- * @param function - a functional component to be styled; must accept a className prop
105
- * @returns a function that accepts a template string of css returns a decorated functional component
66
+ * The core functionality of the exported `styled` Object.
67
+ *
68
+ * Not intended to be used directly. Instead, use the `styled` object.
106
69
  */
107
70
  export function styledBase<C extends FC<allowableAny>>(Component: C) {
108
71
  return (...cssProps: Parameters<typeof css>) => {
@@ -119,6 +82,7 @@ export function styledBase<C extends FC<allowableAny>>(Component: C) {
119
82
  _focusVisible,
120
83
  _focusWithin,
121
84
  _hover,
85
+ _light,
122
86
  _target,
123
87
  _visited,
124
88
  _zx = {},
@@ -179,6 +143,13 @@ export function styledBase<C extends FC<allowableAny>>(Component: C) {
179
143
  }
180
144
  `
181
145
  }
146
+ if (_light) {
147
+ cssStr += `
148
+ @media (prefers-color-scheme: light) {
149
+ ${zxToCss(_light)}
150
+ }
151
+ `
152
+ }
182
153
  if (_target) {
183
154
  cssStr += `
184
155
  &:target {
@@ -218,3 +189,47 @@ export function styledBase<C extends FC<allowableAny>>(Component: C) {
218
189
  return CStyled as unknown as SC<Parameters<C>[0]>
219
190
  }
220
191
  }
192
+
193
+ /**********************
194
+ * Helper Functions
195
+ **********************/
196
+
197
+ /** Expands the shorthand props of a zx prop into css full */
198
+ function expandShorthandProps(zx: Zx) {
199
+ return Object.entries(zx).reduce((acc, [k, v]) => {
200
+ if (k === 'mx') {
201
+ acc.marginLeft = v
202
+ acc.marginRight = v
203
+ } else if (k === 'my') {
204
+ acc.marginTop = v
205
+ acc.marginBottom = v
206
+ } else if (k === 'px') {
207
+ acc.paddingLeft = v
208
+ acc.paddingRight = v
209
+ } else if (k === 'py') {
210
+ acc.paddingTop = v
211
+ acc.paddingBottom = v
212
+ } else if (k in shorthandPropsMap) {
213
+ acc[toCamelCase(shorthandPropsMap[k as keyof typeof shorthandPropsMap])] = v
214
+ } else {
215
+ acc[k] = v
216
+ }
217
+ return acc
218
+ }, {} as Record<string, allowableAny>)
219
+ }
220
+
221
+ /** Converts a zx prop into css string */
222
+ function zxToCss(zx: Zx): string {
223
+ return Object.entries(zx)
224
+ .map(([k, v]) => {
225
+ if (!v) return ''
226
+ k = toKebabCase(k)
227
+ if (typeof v === 'number') v = v + 'px'
228
+ if (Array.isArray(v)) {
229
+ // @ts-expect-error - TS gets confused by the complexity
230
+ v = '[' + v.map(v => (typeof v === 'number' ? v + 'px' : v)).join(',') + ']'
231
+ }
232
+ return k + ':' + v + ';'
233
+ })
234
+ .join('\n')
235
+ }