|
| 1 | +import TestCase from '../../TestCase'; |
| 2 | +import Fixture from '../../Fixture'; |
| 3 | + |
| 4 | +const React = window.React; |
| 5 | +const {Fragment, useEffect, useRef, useState} = React; |
| 6 | + |
| 7 | +export default function GetClientRectsCase() { |
| 8 | + const fragmentRef = useRef(null); |
| 9 | + const [rects, setRects] = useState([]); |
| 10 | + const getRects = () => { |
| 11 | + const rects = fragmentRef.current.getClientRects(); |
| 12 | + setRects(rects); |
| 13 | + }; |
| 14 | + |
| 15 | + return ( |
| 16 | + <TestCase title="getClientRects"> |
| 17 | + <TestCase.Steps> |
| 18 | + <li> |
| 19 | + Click the "Print Rects" button to get the client rects of the |
| 20 | + elements. |
| 21 | + </li> |
| 22 | + </TestCase.Steps> |
| 23 | + <TestCase.ExpectedResult> |
| 24 | + Calling getClientRects on the fragment instance will return a list of a |
| 25 | + DOMRectList for each child node. |
| 26 | + </TestCase.ExpectedResult> |
| 27 | + <Fixture> |
| 28 | + <Fixture.Controls> |
| 29 | + <button onClick={getRects}>Print Rects</button> |
| 30 | + <div style={{display: 'flex'}}> |
| 31 | + <div |
| 32 | + style={{ |
| 33 | + position: 'relative', |
| 34 | + width: '30vw', |
| 35 | + height: '30vh', |
| 36 | + border: '1px solid black', |
| 37 | + }}> |
| 38 | + {rects.map(({x, y, width, height}, index) => { |
| 39 | + const scale = 0.3; |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + key={index} |
| 44 | + style={{ |
| 45 | + position: 'absolute', |
| 46 | + top: y * scale, |
| 47 | + left: x * scale, |
| 48 | + width: width * scale, |
| 49 | + height: height * scale, |
| 50 | + border: '1px solid red', |
| 51 | + boxSizing: 'border-box', |
| 52 | + }}></div> |
| 53 | + ); |
| 54 | + })} |
| 55 | + </div> |
| 56 | + <div> |
| 57 | + {rects.map(({x, y, width, height}, index) => { |
| 58 | + return ( |
| 59 | + <div> |
| 60 | + {index} :: {`{`}x: {x}, y: {y}, width: {width}, height:{' '} |
| 61 | + {height} |
| 62 | + {`}`} |
| 63 | + </div> |
| 64 | + ); |
| 65 | + })} |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </Fixture.Controls> |
| 69 | + <Fragment ref={fragmentRef}> |
| 70 | + <span |
| 71 | + style={{ |
| 72 | + width: '300px', |
| 73 | + height: '250px', |
| 74 | + backgroundColor: 'lightblue', |
| 75 | + fontSize: 20, |
| 76 | + border: '1px solid black', |
| 77 | + marginBottom: '10px', |
| 78 | + }}> |
| 79 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do |
| 80 | + eiusmod tempor incididunt ut labore et dolore magna aliqua. |
| 81 | + </span> |
| 82 | + <div |
| 83 | + style={{ |
| 84 | + width: '150px', |
| 85 | + height: '100px', |
| 86 | + backgroundColor: 'lightgreen', |
| 87 | + border: '1px solid black', |
| 88 | + }}></div> |
| 89 | + <div |
| 90 | + style={{ |
| 91 | + width: '500px', |
| 92 | + height: '50px', |
| 93 | + backgroundColor: 'lightpink', |
| 94 | + border: '1px solid black', |
| 95 | + }}></div> |
| 96 | + </Fragment> |
| 97 | + </Fixture> |
| 98 | + </TestCase> |
| 99 | + ); |
| 100 | +} |
0 commit comments