|
1 | 1 | /**
|
2 |
| - * Internal dependencies |
| 2 | + * External dependencies |
3 | 3 | */
|
4 |
| -import { useCx } from '..'; |
5 |
| -import StyleProvider from '../../../style-provider'; |
| 4 | +import { css } from '@emotion/react'; |
6 | 5 |
|
7 | 6 | /**
|
8 | 7 | * WordPress dependencies
|
9 | 8 | */
|
10 |
| -import { useState, createPortal } from '@wordpress/element'; |
| 9 | +import { __unstableIframe as Iframe } from '@wordpress/block-editor'; |
| 10 | + |
11 | 11 | /**
|
12 |
| - * External dependencies |
| 12 | + * Internal dependencies |
13 | 13 | */
|
14 |
| -import { css } from '@emotion/react'; |
| 14 | +import { useCx } from '..'; |
| 15 | +import StyleProvider from '../../../style-provider'; |
| 16 | +import { |
| 17 | + createSlotFill, |
| 18 | + Provider as SlotFillProvider, |
| 19 | +} from '../../../slot-fill'; |
15 | 20 |
|
16 | 21 | export default {
|
17 | 22 | title: 'Components (Experimental)/useCx',
|
18 | 23 | };
|
19 | 24 |
|
20 |
| -const IFrame = ( { children } ) => { |
21 |
| - const [ iframeDocument, setIframeDocument ] = useState(); |
22 |
| - |
23 |
| - const handleRef = ( node ) => { |
24 |
| - if ( ! node ) { |
25 |
| - return null; |
26 |
| - } |
| 25 | +const Example = ( { args, children } ) => { |
| 26 | + const cx = useCx(); |
| 27 | + const classes = cx( ...args ); |
| 28 | + return <span className={ classes }>{ children }</span>; |
| 29 | +}; |
27 | 30 |
|
28 |
| - function setIfReady() { |
29 |
| - const { contentDocument } = node; |
30 |
| - const { readyState } = contentDocument; |
| 31 | +export const _slotFill = () => { |
| 32 | + const { Fill, Slot } = createSlotFill( 'UseCxExampleSlot' ); |
31 | 33 |
|
32 |
| - if ( readyState !== 'interactive' && readyState !== 'complete' ) { |
33 |
| - return false; |
34 |
| - } |
| 34 | + const redText = css` |
| 35 | + color: red; |
| 36 | + `; |
| 37 | + const blueText = css` |
| 38 | + color: blue; |
| 39 | + `; |
| 40 | + const greenText = css` |
| 41 | + color: green; |
| 42 | + `; |
35 | 43 |
|
36 |
| - setIframeDocument( contentDocument ); |
37 |
| - } |
| 44 | + return ( |
| 45 | + <SlotFillProvider> |
| 46 | + <StyleProvider document={ document }> |
| 47 | + <Iframe> |
| 48 | + <Iframe> |
| 49 | + <Example args={ [ redText ] }> |
| 50 | + This text is inside an iframe and should be red |
| 51 | + </Example> |
| 52 | + <Fill name="test-slot"> |
| 53 | + <Example args={ [ blueText ] }> |
| 54 | + This text is also inside the iframe, but is |
| 55 | + relocated by a slot/fill and should be blue |
| 56 | + </Example> |
| 57 | + </Fill> |
| 58 | + <Fill name="outside-frame"> |
| 59 | + <Example args={ [ greenText ] }> |
| 60 | + This text is also inside the iframe, but is |
| 61 | + relocated by a slot/fill and should be green |
| 62 | + </Example> |
| 63 | + </Fill> |
| 64 | + </Iframe> |
| 65 | + <StyleProvider document={ document }> |
| 66 | + <Slot bubblesVirtually name="test-slot" /> |
| 67 | + </StyleProvider> |
| 68 | + </Iframe> |
| 69 | + <Slot bubblesVirtually name="outside-frame" /> |
| 70 | + </StyleProvider> |
| 71 | + </SlotFillProvider> |
| 72 | + ); |
| 73 | +}; |
38 | 74 |
|
39 |
| - if ( setIfReady() ) { |
40 |
| - return; |
41 |
| - } |
| 75 | +export const _slotFillSimple = () => { |
| 76 | + const { Fill, Slot } = createSlotFill( 'UseCxExampleSlotTwo' ); |
42 | 77 |
|
43 |
| - node.addEventListener( 'load', () => { |
44 |
| - // iframe isn't immediately ready in Firefox |
45 |
| - setIfReady(); |
46 |
| - } ); |
47 |
| - }; |
| 78 | + const redText = css` |
| 79 | + color: red; |
| 80 | + `; |
48 | 81 |
|
49 | 82 | return (
|
50 |
| - <iframe ref={ handleRef } title="use-cx-test-frame"> |
51 |
| - { iframeDocument && |
52 |
| - createPortal( |
53 |
| - <StyleProvider document={ iframeDocument }> |
54 |
| - { children } |
55 |
| - </StyleProvider>, |
56 |
| - iframeDocument.body |
57 |
| - ) } |
58 |
| - </iframe> |
| 83 | + <SlotFillProvider> |
| 84 | + <Iframe> |
| 85 | + <Fill name="test-slot"> |
| 86 | + <Example args={ [ redText ] }> |
| 87 | + This text should be red |
| 88 | + </Example> |
| 89 | + </Fill> |
| 90 | + </Iframe> |
| 91 | + <Slot bubblesVirtually name="test-slot" /> |
| 92 | + </SlotFillProvider> |
59 | 93 | );
|
60 | 94 | };
|
61 | 95 |
|
62 |
| -const Example = ( { args, children } ) => { |
63 |
| - const cx = useCx(); |
64 |
| - const classes = cx( ...args ); |
65 |
| - return <span className={ classes }>{ children }</span>; |
66 |
| -}; |
67 |
| - |
68 | 96 | export const _default = () => {
|
69 | 97 | const redText = css`
|
70 | 98 | color: red;
|
71 | 99 | `;
|
72 | 100 | return (
|
73 |
| - <IFrame> |
| 101 | + <Iframe> |
74 | 102 | <Example args={ [ redText ] }>
|
75 | 103 | This text is inside an iframe and is red!
|
76 | 104 | </Example>
|
77 |
| - </IFrame> |
| 105 | + </Iframe> |
78 | 106 | );
|
79 | 107 | };
|
0 commit comments