Skip to content

Commit c2d7f40

Browse files
committed
refactor: internal cleanup of /lib
1 parent 5190a87 commit c2d7f40

23 files changed

+84
-347
lines changed

cypress/tests/system/create-system.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

cypress/tests/system/hooks/use-config.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

cypress/tests/system/hooks/use-css.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

cypress/tests/system/hooks/use-h.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

cypress/tests/system/hooks/use-icon.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

cypress/tests/system/hooks/use-style-variant.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

cypress/tests/system/hooks/use-styles.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

cypress/tests/system/hooks/use-system.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

cypress/tests/system/hooks/use-theme.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

cypress/tests/util/parse-svg-element.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ import {renderToStaticMarkup} from 'react-dom/server.js';
44
import {parseSvgElement} from '../../../lib/util/parse-svg-element.js';
55

66
describe('parseSvgElement', () => {
7-
const h = () => '<svg>parsed</svg>';
7+
const createElement = () => '<svg>parsed</svg>';
88

99
it('should return null if svg is empty', () => {
10-
expect(parseSvgElement('', {h})).to.equal(null);
11-
expect(parseSvgElement(null, {h})).to.equal(null);
12-
expect(parseSvgElement(undefined, {h})).to.equal(null);
10+
expect(parseSvgElement('', {createElement})).to.equal(null);
11+
expect(parseSvgElement(null, {createElement})).to.equal(null);
12+
expect(parseSvgElement(undefined, {createElement})).to.equal(null);
1313
});
1414

1515
it('should return null if svg fails to parse', () => {
16-
expect(parseSvgElement('bad svg', {h})).to.equal(null);
16+
expect(parseSvgElement('bad svg', {createElement})).to.equal(null);
1717
});
1818

1919
it('should return svg element (react.createElement)', () => {
2020
const svg = '<svg><g><path></path></g></svg>';
2121
expect(
22-
renderToStaticMarkup(parseSvgElement(svg, {h: React.createElement})),
22+
renderToStaticMarkup(
23+
parseSvgElement(svg, {createElement: React.createElement}),
24+
),
2325
).to.equal(svg);
2426
});
2527
});

index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// Components
21
export {Element} from './lib/components/element.js';
32
export {Icon} from './lib/components/icon.js';
43
export {Layout} from './lib/components/layout.js';
54
export {Text} from './lib/components/text.js';
6-
7-
// System
85
export {createSystem} from './lib/system/create-system.js';

lib/components/element.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {useConfig, useCss, useH, useStyleVariant} from '../system/hooks.js';
1+
import {props as p} from 'uinix-fp';
2+
3+
import {getStore} from '../system/store.js';
24
import {mergeClassNames} from '../util/merge-class-names.js';
35
import {resolveShorthandProps} from '../util/resolve-shorthand-props.js';
46

@@ -7,36 +9,32 @@ export const Element = (props) => {
79
as = 'div',
810
children,
911
className = '',
10-
styleProps,
1112
styles,
13+
styleProps,
1214
styleVariant = '',
13-
...rest
15+
...restProps
1416
} = props;
1517

16-
const config = useConfig();
17-
const elementCss = useCss(props);
18-
const styleCss = useCss(styleProps);
19-
const h = useH();
20-
const variantStyle = useStyleVariant(styleVariant);
18+
const store = getStore();
19+
const {config, css, system} = store;
20+
const {createElement, elementShorthandPropsMapping, elementStyles} = config;
21+
22+
const variantStyle = p(styleVariant)(system.styles);
2123

22-
const {elementShorthandPropsMapping, elementStyles} = config;
23-
const [restProps, shorthandStyle] = resolveShorthandProps(
24+
const [rest, shorthandStyle] = resolveShorthandProps(
2425
elementShorthandPropsMapping,
25-
)(rest);
26+
)(restProps);
2627

27-
const mergedStyles = [variantStyle, styles, shorthandStyle].flat();
28+
const mergedStyles = [variantStyle, styles, shorthandStyle].flat(); // order matters
2829
const mergedClassNames = mergeClassNames([
29-
elementCss(elementStyles),
30-
styleCss(mergedStyles),
30+
css(props)(elementStyles),
31+
css(styleProps)(mergedStyles),
3132
className,
3233
]);
3334

34-
const elementProps = {
35-
...restProps,
36-
className: mergedClassNames,
37-
};
35+
const elementProps = {...rest, className: mergedClassNames};
3836

3937
return typeof as === 'string'
40-
? h(as, elementProps, children)
38+
? createElement(as, elementProps, children)
4139
: as(elementProps, children);
4240
};

lib/components/icon.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import {filterEntries} from 'uinix-fp';
1+
import {props as p} from 'uinix-fp';
22

3-
import {useIcon} from '../system/hooks.js';
3+
import {getStore} from '../system/store.js';
4+
import {filterStyle} from '../util/filter-style.js';
5+
import {parseSvgElement} from '../util/parse-svg-element.js';
46
import {Element} from './element.js';
57

68
export const Icon = (props) => {
@@ -9,15 +11,20 @@ export const Icon = (props) => {
911
height,
1012
icon = '',
1113
size,
12-
styleProps,
1314
styles,
15+
styleProps,
1416
width,
1517
...restProps
1618
} = props;
1719

18-
const iconSvg = useIcon(icon);
20+
const {config, system} = getStore();
21+
22+
const {createElement} = config;
1923

20-
const style = filterEntries(([_k, v]) => v !== undefined)({
24+
const svg = p(icon)(system.icons);
25+
const svgElement = parseSvgElement(svg, {createElement});
26+
27+
const style = filterStyle({
2128
color,
2229
display: 'inline-flex',
2330
flex: 'none',
@@ -29,8 +36,8 @@ export const Icon = (props) => {
2936

3037
return Element({
3138
...restProps,
32-
children: iconSvg,
33-
styleProps,
39+
children: svgElement,
3440
styles: [style, styles],
41+
styleProps,
3542
});
3643
};

0 commit comments

Comments
 (0)