Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | import React, { useState } from 'react'; import createClass from 'create-react-class'; import { Meta, Story } from '@storybook/react'; import PropTypes from 'prop-types'; import Portal, { IPortalProps } from './Portal'; import Button from '../Button/Button'; export default { title: 'Utility/Portal', component: Portal, parameters: { docs: { description: { component: Portal.peek.description, }, }, }, } as Meta; /* Basic */ export const Basic: Story<IPortalProps> = (args) => { const [state, setState] = useState({ left: 216, top: 460, }); const handleClick = (event) => { const { height, width } = event.target.getBoundingClientRect(); setState({ left: event.pageX - width / 2, top: event.pageY - height / 2, }); }; const { left, top } = state; return ( <Portal portalId='example-portal123' className='example-portal-container' style={{ width: 128, height: 128, backgroundColor: '#2abbb0', color: '#fff', boxShadow: '0 0 36px rgba(0, 0, 0, 0.5)', transition: 'left .5s, top .5s', display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', cursor: 'pointer', position: 'absolute', left: left, top: top, }} onClick={handleClick} > <section style={{ pointerEvents: 'none', textAlign: 'center', }} > <p>click to move</p> <p> ({left}, {top}) </p> <p>inspect me</p> </section> </Portal> ); }; /* Context */ export const Context: Story<IPortalProps> = () => { const context: any = React.createContext({ display: 'hello', }); class ExampleApp extends React.Component { static propTypes: { children: PropTypes.Requireable<PropTypes.ReactNodeLike>; }; constructor(props: any) { super(props); this.state = { counter: 0, increment: () => { this.setState(({ counter }: any) => ({ counter: counter + 1 })); }, }; } render() { return ( <context.Provider value={this.state}> {this.props.children} </context.Provider> ); } } ExampleApp.propTypes = { children: PropTypes.node, }; const Component = createClass({ render() { return ( <ExampleApp> <context.Consumer> {({ counter, increment }: any) => ( <div> <h1>counter: {counter}</h1> <Button kind='primary' onClick={increment}> increment </Button> </div> )} </context.Consumer> <Portal style={{ marginTop: 20, border: '1px solid #333', padding: 10, width: 100, height: 100, backgroundColor: 'yellow', }} > <context.Consumer> {({ counter, increment }: any) => ( <div> inside the portal counter: {counter} <Button kind='primary' onClick={increment}> increment </Button> </div> )} </context.Consumer> </Portal> </ExampleApp> ); }, }); return <Component />; }; |