-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix Slot/Fill emotion style provider * Add slotfill storybook example to `useCx` * Use iframe from `@wordpress/block-editor`, add `bubblesVirtually` prop * Fix slotfill name * Add simpler slotfill example
- Loading branch information
Showing
2 changed files
with
86 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,107 @@ | ||
/** | ||
* Internal dependencies | ||
* External dependencies | ||
*/ | ||
import { useCx } from '..'; | ||
import StyleProvider from '../../../style-provider'; | ||
import { css } from '@emotion/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, createPortal } from '@wordpress/element'; | ||
import { __unstableIframe as Iframe } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* External dependencies | ||
* Internal dependencies | ||
*/ | ||
import { css } from '@emotion/react'; | ||
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; | ||
} | ||
const Example = ( { args, children } ) => { | ||
const cx = useCx(); | ||
const classes = cx( ...args ); | ||
return <span className={ classes }>{ children }</span>; | ||
}; | ||
|
||
function setIfReady() { | ||
const { contentDocument } = node; | ||
const { readyState } = contentDocument; | ||
export const _slotFill = () => { | ||
const { Fill, Slot } = createSlotFill( 'UseCxExampleSlot' ); | ||
|
||
if ( readyState !== 'interactive' && readyState !== 'complete' ) { | ||
return false; | ||
} | ||
const redText = css` | ||
color: red; | ||
`; | ||
const blueText = css` | ||
color: blue; | ||
`; | ||
const greenText = css` | ||
color: green; | ||
`; | ||
|
||
setIframeDocument( contentDocument ); | ||
} | ||
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 bubblesVirtually name="test-slot" /> | ||
</StyleProvider> | ||
</Iframe> | ||
<Slot bubblesVirtually name="outside-frame" /> | ||
</StyleProvider> | ||
</SlotFillProvider> | ||
); | ||
}; | ||
|
||
if ( setIfReady() ) { | ||
return; | ||
} | ||
export const _slotFillSimple = () => { | ||
const { Fill, Slot } = createSlotFill( 'UseCxExampleSlotTwo' ); | ||
|
||
node.addEventListener( 'load', () => { | ||
// iframe isn't immediately ready in Firefox | ||
setIfReady(); | ||
} ); | ||
}; | ||
const redText = css` | ||
color: red; | ||
`; | ||
|
||
return ( | ||
<iframe ref={ handleRef } title="use-cx-test-frame"> | ||
{ iframeDocument && | ||
createPortal( | ||
<StyleProvider document={ iframeDocument }> | ||
{ children } | ||
</StyleProvider>, | ||
iframeDocument.body | ||
) } | ||
</iframe> | ||
<SlotFillProvider> | ||
<Iframe> | ||
<Fill name="test-slot"> | ||
<Example args={ [ redText ] }> | ||
This text should be red | ||
</Example> | ||
</Fill> | ||
</Iframe> | ||
<Slot bubblesVirtually name="test-slot" /> | ||
</SlotFillProvider> | ||
); | ||
}; | ||
|
||
const Example = ( { args, children } ) => { | ||
const cx = useCx(); | ||
const classes = cx( ...args ); | ||
return <span className={ classes }>{ children }</span>; | ||
}; | ||
|
||
export const _default = () => { | ||
const redText = css` | ||
color: red; | ||
`; | ||
return ( | ||
<IFrame> | ||
<Iframe> | ||
<Example args={ [ redText ] }> | ||
This text is inside an iframe and is red! | ||
</Example> | ||
</IFrame> | ||
</Iframe> | ||
); | ||
}; |