|
11 | 11 | */
|
12 | 12 |
|
13 | 13 | import {Button, Dialog, DialogTrigger, Heading, OverlayArrow, Popover} from 'react-aria-components';
|
14 |
| -import React from 'react'; |
| 14 | +import React, {useEffect, useRef, useState} from 'react'; |
15 | 15 |
|
16 | 16 | export default {
|
17 | 17 | title: 'React Aria Components'
|
@@ -49,6 +49,79 @@ export const PopoverExample = () => (
|
49 | 49 | </DialogTrigger>
|
50 | 50 | );
|
51 | 51 |
|
| 52 | + |
| 53 | +const COUNTDOWN = 5000; |
| 54 | + |
| 55 | +function PopoverTriggerObserver() { |
| 56 | + const buttonRef = useRef<HTMLButtonElement>(null); |
| 57 | + const [countdown, setCountdown] = useState(COUNTDOWN); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + if (countdown > 0) { |
| 61 | + const intervalId = setInterval(() => { |
| 62 | + setCountdown(countdown - 1000); |
| 63 | + }, 1000); |
| 64 | + return () => { |
| 65 | + clearInterval(intervalId); |
| 66 | + }; |
| 67 | + } |
| 68 | + }, [countdown]); |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + const timeoutId = setTimeout(() => { |
| 72 | + if (buttonRef.current) { |
| 73 | + buttonRef.current.style.width = '200px'; |
| 74 | + buttonRef.current.style.height = '50px'; |
| 75 | + } |
| 76 | + }, COUNTDOWN + 1000); |
| 77 | + return () => { |
| 78 | + clearTimeout(timeoutId); |
| 79 | + }; |
| 80 | + }, []); |
| 81 | + |
| 82 | + return ( |
| 83 | + <div style={{marginBottom: 100, display: 'flex', flexDirection: 'column', alignItems: 'center'}}> |
| 84 | + <div> |
| 85 | + <p>The trigger button below will change size in <strong>{Math.floor(countdown / 1000)}s</strong></p> |
| 86 | + </div> |
| 87 | + <DialogTrigger defaultOpen> |
| 88 | + <Button ref={buttonRef}>Open popover</Button> |
| 89 | + <Popover |
| 90 | + placement="bottom start" |
| 91 | + style={{ |
| 92 | + background: 'Canvas', |
| 93 | + color: 'CanvasText', |
| 94 | + border: '1px solid gray', |
| 95 | + padding: 30, |
| 96 | + zIndex: 5 |
| 97 | + }}> |
| 98 | + <Dialog> |
| 99 | + {({close}) => ( |
| 100 | + <form style={{display: 'flex', flexDirection: 'column'}}> |
| 101 | + <Heading slot="title">Sign up</Heading> |
| 102 | + <label> |
| 103 | + First Name: <input placeholder="John" /> |
| 104 | + </label> |
| 105 | + <label> |
| 106 | + Last Name: <input placeholder="Smith" /> |
| 107 | + </label> |
| 108 | + <Button onPress={close} style={{marginTop: 10}}> |
| 109 | + Submit |
| 110 | + </Button> |
| 111 | + </form> |
| 112 | + )} |
| 113 | + </Dialog> |
| 114 | + </Popover> |
| 115 | + </DialogTrigger> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +export const PopoverTriggerObserverExample = { |
| 122 | + render: PopoverTriggerObserver |
| 123 | +}; |
| 124 | + |
52 | 125 | export const PopoverArrowBoundaryOffsetExample = {
|
53 | 126 | args: {
|
54 | 127 | topLeft: 25,
|
|
0 commit comments