-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathuse-cx.js
128 lines (112 loc) · 2.61 KB
/
use-cx.js
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
/**
* External dependencies
*/
import { css } from '@emotion/react';
/**
* WordPress dependencies
*/
import { useState, createPortal } 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 IFrame = ( { children } ) => {
const [ iframeDocument, setIframeDocument ] = useState();
const handleRef = ( node ) => {
if ( ! node ) {
return null;
}
function setIfReady() {
const { contentDocument } = node;
const { readyState } = contentDocument;
if ( readyState !== 'interactive' && readyState !== 'complete' ) {
return false;
}
setIframeDocument( contentDocument );
}
if ( setIfReady() ) {
return;
}
node.addEventListener( 'load', () => {
// iframe isn't immediately ready in Firefox
setIfReady();
} );
};
return (
<iframe ref={ handleRef } title="use-cx-test-frame">
{ iframeDocument &&
createPortal(
<StyleProvider document={ iframeDocument }>
{ children }
</StyleProvider>,
iframeDocument.body
) }
</iframe>
);
};
const Example = ( { args, children } ) => {
const cx = useCx();
const classes = cx( ...args );
return <span className={ classes }>{ children }</span>;
};
export const _slotfill = () => {
const { Fill, Slot } = createSlotFill( 'ToolsPanelSlot' );
const redText = css`
color: red;
`;
const blueText = css`
color: blue;
`;
const greenText = css`
color: green;
`;
return (
<SlotFillProvider>
<StyleProvider document={ document }>
<IFrame>
<IFrame>
<Example args={ [ redText ] }>
This text is inside an iframe and should be red
</Example>
<Fill name="test-slot">
<Example args={ [ 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 args={ [ 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 name="test-slot" />
</StyleProvider>
</IFrame>
<Slot name="outside-frame" />
</StyleProvider>
</SlotFillProvider>
);
};
export const _default = () => {
const redText = css`
color: red;
`;
return (
<IFrame>
<Example args={ [ redText ] }>
This text is inside an iframe and is red!
</Example>
</IFrame>
);
};