|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { RangeControl, Button } from '@wordpress/components'; |
| 5 | +import { __ } from '@wordpress/i18n'; |
| 6 | +import { useViewportMatch } from '@wordpress/compose'; |
| 7 | +import { plus, lineSolid } from '@wordpress/icons'; |
| 8 | +import { useEffect } from '@wordpress/element'; |
| 9 | + |
| 10 | +const viewportBreaks = { |
| 11 | + xhuge: { min: 3, max: 6, default: 5 }, |
| 12 | + huge: { min: 2, max: 4, default: 4 }, |
| 13 | + xlarge: { min: 2, max: 3, default: 3 }, |
| 14 | + large: { min: 1, max: 2, default: 2 }, |
| 15 | + mobile: { min: 1, max: 2, default: 2 }, |
| 16 | +}; |
| 17 | + |
| 18 | +function useViewPortBreakpoint() { |
| 19 | + const isXHuge = useViewportMatch( 'xhuge', '>=' ); |
| 20 | + const isHuge = useViewportMatch( 'huge', '>=' ); |
| 21 | + const isXlarge = useViewportMatch( 'xlarge', '>=' ); |
| 22 | + const isLarge = useViewportMatch( 'large', '>=' ); |
| 23 | + const isMobile = useViewportMatch( 'mobile', '>=' ); |
| 24 | + |
| 25 | + if ( isXHuge ) { |
| 26 | + return 'xhuge'; |
| 27 | + } |
| 28 | + if ( isHuge ) { |
| 29 | + return 'huge'; |
| 30 | + } |
| 31 | + if ( isXlarge ) { |
| 32 | + return 'xlarge'; |
| 33 | + } |
| 34 | + if ( isLarge ) { |
| 35 | + return 'large'; |
| 36 | + } |
| 37 | + if ( isMobile ) { |
| 38 | + return 'mobile'; |
| 39 | + } |
| 40 | + return null; |
| 41 | +} |
| 42 | + |
| 43 | +// Value is number from 0 to 100 representing how big an item is in the grid |
| 44 | +// 100 being the biggest and 0 being the smallest. |
| 45 | +// The size is relative to the viewport size, if one a given viewport the |
| 46 | +// number of allowed items in a grid is 3 to 6 a 0 ( the smallest ) will mean that the grid will |
| 47 | +// have 6 items in a row, a 100 ( the biggest ) will mean that the grid will have 3 items in a row. |
| 48 | +// A value of 75 will mean that the grid will have 4 items in a row. |
| 49 | +function getRangeValue( |
| 50 | + density: number, |
| 51 | + breakValues: { min: number; max: number; default: number } |
| 52 | +) { |
| 53 | + const inverseDensity = breakValues.max - density; |
| 54 | + const max = breakValues.max - breakValues.min; |
| 55 | + return Math.round( ( inverseDensity * 100 ) / max ); |
| 56 | +} |
| 57 | + |
| 58 | +export default function DensityPicker( { |
| 59 | + density, |
| 60 | + setDensity, |
| 61 | +}: { |
| 62 | + density: number; |
| 63 | + setDensity: React.Dispatch< React.SetStateAction< number > >; |
| 64 | +} ) { |
| 65 | + const viewport = useViewPortBreakpoint(); |
| 66 | + useEffect( () => { |
| 67 | + setDensity( ( _density ) => { |
| 68 | + if ( ! viewport || ! _density ) { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + const breakValues = viewportBreaks[ viewport ]; |
| 72 | + if ( _density < breakValues.min ) { |
| 73 | + return breakValues.min; |
| 74 | + } |
| 75 | + if ( _density > breakValues.max ) { |
| 76 | + return breakValues.max; |
| 77 | + } |
| 78 | + return _density; |
| 79 | + } ); |
| 80 | + }, [ setDensity, viewport ] ); |
| 81 | + if ( ! viewport ) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + const breakValues = viewportBreaks[ viewport ]; |
| 85 | + const densityToUse = density || breakValues.default; |
| 86 | + const rangeValue = getRangeValue( densityToUse, breakValues ); |
| 87 | + |
| 88 | + const step = 100 / ( breakValues.max - breakValues.min + 1 ); |
| 89 | + return ( |
| 90 | + <> |
| 91 | + <Button |
| 92 | + size="compact" |
| 93 | + icon={ lineSolid } |
| 94 | + disabled={ rangeValue <= 0 } |
| 95 | + accessibleWhenDisabled |
| 96 | + label={ __( 'Decrease size' ) } |
| 97 | + onClick={ () => { |
| 98 | + setDensity( densityToUse + 1 ); |
| 99 | + } } |
| 100 | + /> |
| 101 | + <RangeControl |
| 102 | + __nextHasNoMarginBottom |
| 103 | + showTooltip={ false } |
| 104 | + className="dataviews-density-picker__range-control" |
| 105 | + label={ __( 'Item size' ) } |
| 106 | + hideLabelFromVision |
| 107 | + value={ rangeValue } |
| 108 | + min={ 0 } |
| 109 | + max={ 100 } |
| 110 | + withInputField={ false } |
| 111 | + onChange={ ( value = 0 ) => { |
| 112 | + const inverseValue = 100 - value; |
| 113 | + setDensity( |
| 114 | + Math.round( |
| 115 | + ( inverseValue * |
| 116 | + ( breakValues.max - breakValues.min ) ) / |
| 117 | + 100 + |
| 118 | + breakValues.min |
| 119 | + ) |
| 120 | + ); |
| 121 | + } } |
| 122 | + step={ step } |
| 123 | + /> |
| 124 | + <Button |
| 125 | + size="compact" |
| 126 | + icon={ plus } |
| 127 | + disabled={ rangeValue >= 100 } |
| 128 | + accessibleWhenDisabled |
| 129 | + label={ __( 'Increase size' ) } |
| 130 | + onClick={ () => { |
| 131 | + setDensity( densityToUse - 1 ); |
| 132 | + } } |
| 133 | + /> |
| 134 | + </> |
| 135 | + ); |
| 136 | +} |
0 commit comments