Skip to content

Commit 675df2c

Browse files
committed
feat(hooks): support props path in hooks
1 parent 00a8e89 commit 675df2c

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

cypress/fixtures/test-system.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
const icon =
2+
'<svg xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>';
3+
14
const system = {
25
icons: {
3-
x: '<svg xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>',
6+
x: icon,
47
nested: {
5-
x: '<svg xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>',
8+
x: icon,
69
},
10+
'a.b.c': icon,
711
},
812
styles: {
913
red: {
@@ -104,6 +108,9 @@ const system = {
104108
m: 16,
105109
l: 32,
106110
xl: 64,
111+
scale: {
112+
4: 16,
113+
},
107114
},
108115
sizes: {
109116
icon: {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ describe('useIcon', () => {
4141
it('should return an SVG element for the specified nested icon from the system', () => {
4242
load({h, system});
4343
mount(<CustomElement icon="nested.x" />);
44-
4544
const svg = system.icons.nested.x;
4645
const svgElement = parseSvgElement({h, svg});
4746
cy.get('@iconSvg').should((iconSvg) => {
@@ -50,4 +49,16 @@ describe('useIcon', () => {
5049
);
5150
});
5251
});
52+
53+
it('should return an SVG element for the specified direct icon from the system', () => {
54+
load({h, system});
55+
mount(<CustomElement icon="a.b.c" />);
56+
const svg = system.icons['a.b.c'];
57+
const svgElement = parseSvgElement({h, svg});
58+
cy.get('@iconSvg').should((iconSvg) => {
59+
expect(renderToStaticMarkup(iconSvg)).to.equal(
60+
renderToStaticMarkup(svgElement),
61+
);
62+
});
63+
});
5364
});

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import React, {createElement as h} from 'react';
44
import {createTheme, load, useTheme} from '../../../../index.js';
55
import system from '../../../fixtures/test-system.js';
66

7-
const CustomElement = () => {
8-
const theme = useTheme();
7+
const CustomElement = ({path = ''}) => {
8+
const theme = useTheme(path);
99
cy.wrap(theme).as('theme');
1010
return <pre>{JSON.stringify(theme, null, 2)}</pre>;
1111
};
@@ -18,7 +18,33 @@ describe('useTheme', () => {
1818
it('should retrieve the theme from the system', () => {
1919
load({h, system});
2020
mount(<CustomElement />);
21-
2221
cy.get('@theme').should('deep.equal', createTheme(system.theme));
2322
});
23+
24+
it('should retrieve the theme values given a property path', () => {
25+
load({h, system});
26+
mount(<CustomElement path="keyframes.flicker" />);
27+
cy.get('@theme').should(
28+
'deep.equal',
29+
createTheme(system.theme).keyframes.flicker,
30+
);
31+
32+
mount(<CustomElement path="colors.palette" />);
33+
cy.get('@theme').should(
34+
'deep.equal',
35+
createTheme(system.theme).colors.palette,
36+
);
37+
38+
mount(<CustomElement path="colors.palette.red1" />);
39+
cy.get('@theme').should(
40+
'deep.equal',
41+
createTheme(system.theme).colors.palette.red1,
42+
);
43+
44+
mount(<CustomElement path="spacings.4" />);
45+
cy.get('@theme').should(
46+
'deep.equal',
47+
createTheme(system.theme).spacings[4],
48+
);
49+
});
2450
});

lib/system/hooks.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,18 @@ const useSystem = () => getStore().system;
7979
/**
8080
* Retrieves `system.theme`.
8181
*
82+
* @param {string} [path]
83+
* Theme property path
8284
* @returns {SystemTheme}
8385
*/
84-
const useTheme = () => useSystem().theme;
86+
const useTheme = (path = '') => props(path)(useSystem().theme);
8587

8688
/**
8789
* Retrieves the typography variant style in
8890
* `system.styles.typography.variants[variant]`.
8991
*
9092
* @param {string} variant
91-
* variant property path
93+
* Variant property path
9294
* @returns {StyleDefinition|void}
9395
*/
9496
const useTypographyVariant = (variant) =>
@@ -98,7 +100,7 @@ const useTypographyVariant = (variant) =>
98100
* Retrieves the variant style in `system.styles.variants[variant]`.
99101
*
100102
* @param {string} variant
101-
* variant property path
103+
* Variant property path
102104
* @returns {StyleDefinition|void}
103105
*/
104106
const useVariant = (variant) =>

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"fela-preset-web": "^11.7.0",
4040
"hast-to-hyperscript": "^10.0.1",
4141
"svg-parser": "^2.0.4",
42+
"uinix-fp": "^0.2.4",
4243
"uinix-theme": "^0.2.3"
4344
},
4445
"devDependencies": {
@@ -47,31 +48,31 @@
4748
"@babel/preset-react": "^7.13.13",
4849
"@cypress/code-coverage": "^3.9.11",
4950
"@cypress/react": "^5.10.1",
50-
"@cypress/webpack-dev-server": "^1.6.0",
51-
"@types/react": "^17.0.27",
51+
"@cypress/webpack-dev-server": "^1.7.0",
52+
"@types/react": "^17.0.30",
5253
"@types/svg-parser": "^2.0.2",
5354
"@types/tape": "^4.13.2",
5455
"babel-loader": "^8.2.2",
55-
"babel-plugin-istanbul": "^6.0.0",
56+
"babel-plugin-istanbul": "^6.1.1",
5657
"c8": "^7.10.0",
57-
"cypress": "^8.5.0",
58+
"cypress": "^8.6.0",
5859
"eslint-config-xo-react": "^0.25.0",
5960
"eslint-plugin-cypress": "^2.12.1",
6061
"eslint-plugin-react": "^7.26.1",
6162
"eslint-plugin-react-hooks": "^4.2.0",
6263
"htm": "^3.1.0",
63-
"html-webpack-plugin": "^5.3.1",
64+
"html-webpack-plugin": "^5.4.0",
6465
"hyperscript": "^2.0.2",
6566
"mithril": "^2.0.4",
66-
"preact": "^10.5.14",
67+
"preact": "^10.5.15",
6768
"react": "^17.0.2",
6869
"react-dom": "^17.0.2",
6970
"solid-js": "^1.1.6",
7071
"tape": "^5.3.1",
7172
"type-coverage": "^2.18.2",
72-
"typescript": "^4.4.3",
73+
"typescript": "^4.4.4",
7374
"uinix-scripts": "^0.1.2",
74-
"webpack": "^5.58.1",
75+
"webpack": "^5.58.2",
7576
"webpack-dev-server": "^4.3.1",
7677
"xo": "^0.45.0"
7778
},

0 commit comments

Comments
 (0)