|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { storiesOf } from '@storybook/react'; |
| 4 | + |
| 5 | +import { createHtmlPortalNode, createSvgPortalNode, InPortal, OutPortal } from '..'; |
| 6 | + |
| 7 | +storiesOf('SVG Portals', module) |
| 8 | + .add('works with SVGs', () => { |
| 9 | + const portalNode = createSvgPortalNode(); |
| 10 | + |
| 11 | + // From https://github.com/httptoolkit/react-reverse-portal/issues/2 |
| 12 | + return <div> |
| 13 | + <svg> |
| 14 | + <rect x={0} y={0} width={300} height={50} fill="gray"></rect> |
| 15 | + <rect x={0} y={50} width={300} height={50} fill="lightblue"></rect> |
| 16 | + <svg x={30} y={10}> |
| 17 | + <InPortal node={portalNode}> |
| 18 | + <text alignmentBaseline="text-before-edge" dominantBaseline="hanging" fill="red"> |
| 19 | + test |
| 20 | + </text> |
| 21 | + </InPortal> |
| 22 | + </svg> |
| 23 | + <svg x={30} y={70}> |
| 24 | + <OutPortal node={portalNode} /> |
| 25 | + </svg> |
| 26 | + </svg> |
| 27 | + </div> |
| 28 | + |
| 29 | + }) |
| 30 | + .add('can move content around within SVGs', () => { |
| 31 | + const portalNode = createSvgPortalNode(); |
| 32 | + |
| 33 | + return React.createElement(() => { |
| 34 | + const [inFirstSvg, setSvgToUse] = React.useState(false); |
| 35 | + |
| 36 | + return <div> |
| 37 | + <button onClick={() => setSvgToUse(!inFirstSvg)}> |
| 38 | + Click to move the OutPortal within the SVG |
| 39 | + </button> |
| 40 | + |
| 41 | + <hr/> |
| 42 | + |
| 43 | + <svg> |
| 44 | + <InPortal node={portalNode}> |
| 45 | + <text alignmentBaseline="text-before-edge" dominantBaseline="hanging" fill="red"> |
| 46 | + test |
| 47 | + </text> |
| 48 | + </InPortal> |
| 49 | + |
| 50 | + <rect x={0} y={0} width={300} height={50} fill="gray"></rect> |
| 51 | + <rect x={0} y={50} width={300} height={50} fill="lightblue"></rect> |
| 52 | + |
| 53 | + <svg x={30} y={10}> |
| 54 | + { inFirstSvg && <OutPortal node={portalNode} /> } |
| 55 | + </svg> |
| 56 | + <svg x={30} y={70}> |
| 57 | + { !inFirstSvg && <OutPortal node={portalNode} /> } |
| 58 | + </svg> |
| 59 | + </svg> |
| 60 | + </div> |
| 61 | + }); |
| 62 | + }) |
| 63 | + .add('persist DOM while moving within SVGs', () => { |
| 64 | + const portalNode = createSvgPortalNode(); |
| 65 | + |
| 66 | + return React.createElement(() => { |
| 67 | + const [inFirstSvg, setSvgToUse] = React.useState(false); |
| 68 | + |
| 69 | + return <div> |
| 70 | + <button onClick={() => setSvgToUse(!inFirstSvg)}> |
| 71 | + Click to move the OutPortal within the SVG |
| 72 | + </button> |
| 73 | + |
| 74 | + <hr/> |
| 75 | + |
| 76 | + <svg width={500} height={800}> |
| 77 | + <InPortal node={portalNode}> |
| 78 | + <foreignObject width="480" height="360"> |
| 79 | + <video src="https://media.giphy.com/media/l0HlKghz8IvrQ8TYY/giphy.mp4" controls loop /> |
| 80 | + </foreignObject> |
| 81 | + </InPortal> |
| 82 | + |
| 83 | + <rect x={0} y={0} width={500} height={380} fill="gray"></rect> |
| 84 | + <rect x={0} y={380} width={500} height={380} fill="lightblue"></rect> |
| 85 | + |
| 86 | + <svg x={10} y={10}> |
| 87 | + { inFirstSvg && <OutPortal node={portalNode} /> } |
| 88 | + </svg> |
| 89 | + <svg x={10} y={410}> |
| 90 | + { !inFirstSvg && <OutPortal node={portalNode} /> } |
| 91 | + </svg> |
| 92 | + </svg> |
| 93 | + </div> |
| 94 | + }) |
| 95 | + }); |
0 commit comments