-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathuse-cx.js
More file actions
157 lines (142 loc) · 3.6 KB
/
use-cx.js
File metadata and controls
157 lines (142 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* External dependencies
*/
import { css } from '@emotion/react';
/**
* WordPress dependencies
*/
import { __unstableIframe as Iframe } from '@wordpress/block-editor';
import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useCx } from '..';
import StyleProvider from '../../../style-provider';
import {
createSlotFill,
Provider as SlotFillProvider,
} from '../../../slot-fill';
export default {
title: 'Components (Experimental)/useCx',
};
const Example = ( { serializedStyles, children } ) => {
const cx = useCx();
const classes = cx( serializedStyles );
return <span className={ classes }>{ children }</span>;
};
const ExampleWithUseMemoWrong = ( { serializedStyles, children } ) => {
const cx = useCx();
// Wrong: using 'useMemo' without adding 'cx' to the dependency list.
const classes = useMemo(
() => cx( serializedStyles ),
// eslint-disable-next-line react-hooks/exhaustive-deps
[ serializedStyles ]
);
return <span className={ classes }>{ children }</span>;
};
const ExampleWithUseMemoRight = ( { serializedStyles, children } ) => {
const cx = useCx();
// Right: using 'useMemo' with 'cx' listed as a dependency.
const classes = useMemo(
() => cx( serializedStyles ),
[ cx, serializedStyles ]
);
return <span className={ classes }>{ children }</span>;
};
export const _slotFill = () => {
const { Fill, Slot } = createSlotFill( 'UseCxExampleSlot' );
const redText = css`
color: red;
`;
const blueText = css`
color: blue;
`;
const greenText = css`
color: green;
`;
return (
<SlotFillProvider>
<StyleProvider document={ document }>
<Iframe>
<Iframe>
<Example serializedStyles={ redText }>
This text is inside an iframe and should be red
</Example>
<Fill name="test-slot">
<Example serializedStyles={ blueText }>
This text is also inside the iframe, but is
relocated by a slot/fill and should be blue
</Example>
</Fill>
<Fill name="outside-frame">
<Example serializedStyles={ greenText }>
This text is also inside the iframe, but is
relocated by a slot/fill and should be green
</Example>
</Fill>
</Iframe>
<StyleProvider document={ document }>
<Slot bubblesVirtually name="test-slot" />
</StyleProvider>
</Iframe>
<Slot bubblesVirtually name="outside-frame" />
</StyleProvider>
</SlotFillProvider>
);
};
export const _slotFillSimple = () => {
const { Fill, Slot } = createSlotFill( 'UseCxExampleSlotTwo' );
const redText = css`
color: red;
`;
return (
<SlotFillProvider>
<Iframe>
<Fill name="test-slot">
<Example serializedStyles={ redText }>
This text should be red
</Example>
</Fill>
</Iframe>
<Slot bubblesVirtually name="test-slot" />
</SlotFillProvider>
);
};
export const _useMemoBadPractices = () => {
const redText = css`
color: red;
`;
const blueText = css`
color: blue;
`;
return (
<>
<Example serializedStyles={ redText }>
This text should be red
</Example>
<ExampleWithUseMemoRight serializedStyles={ blueText }>
This text should be blue
</ExampleWithUseMemoRight>
<Iframe>
<Example serializedStyles={ redText }>
This text should be red
</Example>
<ExampleWithUseMemoWrong serializedStyles={ blueText }>
This text should be blue but it's not!
</ExampleWithUseMemoWrong>
</Iframe>
</>
);
};
export const _default = () => {
const redText = css`
color: red;
`;
return (
<Iframe>
<Example serializedStyles={ redText }>
This text is inside an iframe and is red!
</Example>
</Iframe>
);
};