|
| 1 | +import 'react-app-polyfill/ie11'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | +import * as ReactDOM from 'react-dom'; |
| 5 | +import ResponsiveToolbar, { OverflowButtonProps } from '../../src'; |
| 6 | +import Portal from './Portal'; |
| 7 | +import Button from './Button'; |
| 8 | + |
| 9 | +interface CustomOverflowButtonProps extends OverflowButtonProps { |
| 10 | + overflowMenuOpen: boolean; |
| 11 | + toggleOverflowMenu: () => void; |
| 12 | +} |
| 13 | + |
| 14 | +const OverflowButton: React.FC<CustomOverflowButtonProps> = ({ |
| 15 | + children, |
| 16 | + shown, |
| 17 | + overflowMenuOpen, |
| 18 | + toggleOverflowMenu, |
| 19 | +}) => { |
| 20 | + const buttonRef = React.useRef<HTMLButtonElement>(null); |
| 21 | + |
| 22 | + return ( |
| 23 | + <> |
| 24 | + <button |
| 25 | + onClick={toggleOverflowMenu} |
| 26 | + ref={buttonRef} |
| 27 | + style={{ |
| 28 | + marginInline: 2, |
| 29 | + }} |
| 30 | + > |
| 31 | + Show More ... |
| 32 | + </button> |
| 33 | + {overflowMenuOpen && shown && ( |
| 34 | + <Portal> |
| 35 | + <div |
| 36 | + style={{ |
| 37 | + marginTop: 10, |
| 38 | + display: 'flex', |
| 39 | + flexDirection: 'column', |
| 40 | + alignItems: 'flex-end', |
| 41 | + padding: '10px', |
| 42 | + border: 'thin solid #ccc', |
| 43 | + borderRadius: 3, |
| 44 | + background: '#eee', |
| 45 | + position: 'fixed', |
| 46 | + top: buttonRef.current?.getBoundingClientRect().bottom || 20, |
| 47 | + left: buttonRef.current?.getBoundingClientRect().right || 20, |
| 48 | + transform: 'translateX(-100%)', |
| 49 | + }} |
| 50 | + > |
| 51 | + {React.Children.map(children, child => { |
| 52 | + if (!React.isValidElement(child)) { |
| 53 | + return child; |
| 54 | + } |
| 55 | + |
| 56 | + return React.cloneElement(child, { |
| 57 | + style: { ...child.props.style, width: 'auto' }, |
| 58 | + }); |
| 59 | + })} |
| 60 | + </div> |
| 61 | + </Portal> |
| 62 | + )} |
| 63 | + </> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +const App: React.VFC = () => { |
| 68 | + const [overflowMenuOpen, setOverflowMenuOpen] = useState<boolean>(false); |
| 69 | + const [activeIndex, setActiveIndex] = useState<number | null>(null); |
| 70 | + const [amountButtons, setAmountButtons] = useState<number>(15); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + if (activeIndex !== null && activeIndex + 1 > amountButtons) { |
| 74 | + setActiveIndex(null); |
| 75 | + } |
| 76 | + }, [activeIndex, amountButtons]); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="App"> |
| 80 | + <h1>Responsive Toolbar Example</h1> |
| 81 | + |
| 82 | + <button onClick={() => setAmountButtons(prevAmount => prevAmount + 1)}> |
| 83 | + Add Button |
| 84 | + </button> |
| 85 | + <button |
| 86 | + onClick={() => |
| 87 | + setAmountButtons(prevAmount => Math.max(0, prevAmount - 1)) |
| 88 | + } |
| 89 | + disabled={amountButtons === 0} |
| 90 | + > |
| 91 | + Remove Button |
| 92 | + </button> |
| 93 | + |
| 94 | + <hr /> |
| 95 | + |
| 96 | + <ResponsiveToolbar |
| 97 | + activeIndex={activeIndex} |
| 98 | + overflowButton={OverflowButton} |
| 99 | + overflowButtonProps={{ |
| 100 | + overflowMenuOpen, |
| 101 | + toggleOverflowMenu: () => setOverflowMenuOpen(prevOpen => !prevOpen), |
| 102 | + }} |
| 103 | + > |
| 104 | + {new Array(amountButtons).fill(null).map((_item, index) => ( |
| 105 | + <Button |
| 106 | + key={index} |
| 107 | + active={index === activeIndex} |
| 108 | + onClick={() => { |
| 109 | + setActiveIndex(prevIndex => (prevIndex === index ? null : index)); |
| 110 | + setOverflowMenuOpen(false); |
| 111 | + }} |
| 112 | + > |
| 113 | + {`Button ${index + 1}`} |
| 114 | + </Button> |
| 115 | + ))} |
| 116 | + </ResponsiveToolbar> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +ReactDOM.render(<App />, document.getElementById('root')); |
0 commit comments