Skip to content

Commit

Permalink
Update: Move item size control to the new view config UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Aug 8, 2024
1 parent c48075b commit f8e9334
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 60 deletions.
40 changes: 32 additions & 8 deletions packages/dataviews/src/components/dataviews-view-config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ import warning from '@wordpress/warning';
/**
* Internal dependencies
*/
import { SORTING_DIRECTIONS, sortIcons, sortLabels } from '../../constants';
import {
SORTING_DIRECTIONS,
LAYOUT_GRID,
sortIcons,
sortLabels,
} from '../../constants';
import { VIEW_LAYOUTS, getMandatoryFields } from '../../dataviews-layouts';
import type { SupportedLayouts } from '../../types';
import DataViewsContext from '../dataviews-context';
import { unlock } from '../../lock-unlock';
import DensityPicker from '../../dataviews-layouts/grid/density-picker';

const {
DropdownMenuV2: DropdownMenu,
Expand Down Expand Up @@ -101,10 +107,6 @@ function ViewTypeMenu( {
);
}

interface ViewActionsProps {
defaultLayouts?: SupportedLayouts;
}

function SortFieldControl() {
const { view, fields, onChangeView } = useContext( DataViewsContext );
const orderOptions = useMemo( () => {
Expand Down Expand Up @@ -304,14 +306,27 @@ function SettingsSection( {
);
}

function DataviewsViewConfigContent() {
function DataviewsViewConfigContent( {
density,
setDensity,
}: {
density: number;
setDensity: React.Dispatch< React.SetStateAction< number > >;
} ) {
const { view } = useContext( DataViewsContext );
return (
<VStack className="dataviews-view-config" spacing={ 6 }>
<SettingsSection title={ __( 'Appearance' ) }>
<HStack expanded className="is-divided-in-two">
<SortFieldControl />
<SortDirectionControl />
</HStack>
{ view.type === LAYOUT_GRID && (
<DensityPicker
density={ density }
setDensity={ setDensity }
/>
) }
<ItemsPerPageControl />
</SettingsSection>
<SettingsSection title={ __( 'Properties' ) }>
Expand All @@ -322,8 +337,14 @@ function DataviewsViewConfigContent() {
}

function _DataViewsViewConfig( {
density,
setDensity,
defaultLayouts = { list: {}, grid: {}, table: {} },
}: ViewActionsProps ) {
}: {
density: number;
setDensity: React.Dispatch< React.SetStateAction< number > >;
defaultLayouts?: SupportedLayouts;
} ) {
const [ isShowingViewPopover, setIsShowingViewPopover ] =
useState< boolean >( false );

Expand All @@ -345,7 +366,10 @@ function _DataViewsViewConfig( {
} }
focusOnMount
>
<DataviewsViewConfigContent />
<DataviewsViewConfigContent
density={ density }
setDensity={ setDensity }
/>
</Popover>
) }
</div>
Expand Down
8 changes: 2 additions & 6 deletions packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ export default function DataViews< Item >( {
isShowingFilter={ isShowingFilter }
/>
</HStack>
{ view.type === LAYOUT_GRID && (
<DensityPicker
density={ density }
setDensity={ setDensity }
/>
) }
<DataViewsBulkActions />
<HStack
spacing={ 1 }
Expand All @@ -147,6 +141,8 @@ export default function DataViews< Item >( {
>
<DataViewsViewConfig
defaultLayouts={ defaultLayouts }
density={ density }
setDensity={ setDensity }
/>
{ header }
</HStack>
Expand Down
102 changes: 56 additions & 46 deletions packages/dataviews/src/dataviews-layouts/grid/density-picker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* WordPress dependencies
*/
import { RangeControl, Button } from '@wordpress/components';
import {
BaseControl,
RangeControl,
Button,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useViewportMatch } from '@wordpress/compose';
import { plus, reset } from '@wordpress/icons';
Expand Down Expand Up @@ -87,50 +92,55 @@ export default function DensityPicker( {

const step = 100 / ( breakValues.max - breakValues.min + 1 );
return (
<>
<Button
size="compact"
icon={ reset }
disabled={ rangeValue <= 0 }
accessibleWhenDisabled
label={ __( 'Decrease size' ) }
onClick={ () => {
setDensity( densityToUse + 1 );
} }
/>
<RangeControl
__nextHasNoMarginBottom
showTooltip={ false }
className="dataviews-density-picker__range-control"
label={ __( 'Item size' ) }
hideLabelFromVision
value={ rangeValue }
min={ 0 }
max={ 100 }
withInputField={ false }
onChange={ ( value = 0 ) => {
const inverseValue = 100 - value;
setDensity(
Math.round(
( inverseValue *
( breakValues.max - breakValues.min ) ) /
100 +
breakValues.min
)
);
} }
step={ step }
/>
<Button
size="compact"
icon={ plus }
disabled={ rangeValue >= 100 }
accessibleWhenDisabled
label={ __( 'Increase size' ) }
onClick={ () => {
setDensity( densityToUse - 1 );
} }
/>
</>
<BaseControl>
<BaseControl.VisualLabel>
{ __( 'Item size' ) }
</BaseControl.VisualLabel>
<HStack>
<Button
size="compact"
icon={ reset }
disabled={ rangeValue <= 0 }
accessibleWhenDisabled
label={ __( 'Decrease size' ) }
onClick={ () => {
setDensity( densityToUse + 1 );
} }
/>
<RangeControl
__nextHasNoMarginBottom
showTooltip={ false }
className="dataviews-density-picker__range-control"
label={ __( 'Item size' ) }
hideLabelFromVision
value={ rangeValue }
min={ 0 }
max={ 100 }
withInputField={ false }
onChange={ ( value = 0 ) => {
const inverseValue = 100 - value;
setDensity(
Math.round(
( inverseValue *
( breakValues.max - breakValues.min ) ) /
100 +
breakValues.min
)
);
} }
step={ step }
/>
<Button
size="compact"
icon={ plus }
disabled={ rangeValue >= 100 }
accessibleWhenDisabled
label={ __( 'Increase size' ) }
onClick={ () => {
setDensity( densityToUse - 1 );
} }
/>
</HStack>
</BaseControl>
);
}

0 comments on commit f8e9334

Please sign in to comment.