Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix render stability of IconPicker grid and update usage of deprecated Dropdown property #230

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions components/icon-picker/icon-picker-toolbar-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ const StyledIconPickerDropdown = styled(IconPicker)`
* @returns {*}
*/
export const IconPickerToolbarButton = (props) => {
const {
value: { name, iconSet },
buttonLabel,
} = props;
const { value, buttonLabel } = props;

const buttonIcon = name && iconSet ? <Icon name={name} iconSet={iconSet} /> : null;
const buttonIcon =
value?.name && value?.iconSet ? <Icon name={value?.name} iconSet={value?.iconSet} /> : null;

return (
<Dropdown
className="component-icon-picker-toolbar-button"
contentClassName="component-icon-picker-toolbar-button__content"
position="bottom right"
popoverProps={{
placement: 'bottom-start',
}}
renderToggle={({ isOpen, onToggle }) => (
<ToolbarButton onClick={onToggle} aria-expanded={isOpen} icon={buttonIcon}>
{buttonLabel ?? __('Select Icon')}
Expand Down
56 changes: 38 additions & 18 deletions components/icon-picker/icon-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SearchControl,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useState, memo } from '@wordpress/element';
import { useState } from '@wordpress/element';

import { useIcons } from '../../hooks/use-icons';
import { useFilteredList } from '../../hooks/use-filtered-list';
Expand All @@ -35,9 +35,9 @@ const StyledIconGrid = styled(NavigableMenu)`
`;

const StyledIconButton = styled(Icon)`
background-color: ${({ isSelected }) => (isSelected ? 'black' : 'white')};
color: ${({ isSelected }) => (isSelected ? 'white' : 'black')};
fill: ${({ isSelected }) => (isSelected ? 'white' : 'black')};
background-color: ${({ selected }) => (selected ? 'black' : 'white')};
color: ${({ selected }) => (selected ? 'white' : 'black')};
fill: ${({ selected }) => (selected ? 'white' : 'black')};
padding: 5px;
border: none;
border-radius: 4px;
Expand All @@ -48,7 +48,7 @@ const StyledIconButton = styled(Icon)`
justify-content: center;

&:hover {
background-color: ${({ isSelected }) => (isSelected ? '#555D66' : '#f3f4f5')};
background-color: ${({ selected }) => (selected ? '#555D66' : '#f3f4f5')};
}

& svg {
Expand Down Expand Up @@ -108,29 +108,49 @@ IconPicker.propTypes = {
label: PropTypes.string,
};

/**
* IconLabel
*
* @typedef IconLabelProps
* @property {object} icon icon object
* @property {boolean} isChecked whether the icon is checked
*
* @param {IconLabelProps} props IconLabel Props
* @returns {*} React Element
*/
const IconLabel = (props) => {
const { icon, isChecked } = props;
return (
<>
<StyledIconButton
selected={isChecked}
key={icon.name}
name={icon.name}
iconSet={icon.iconSet}
/>
<VisuallyHidden>{icon.label}</VisuallyHidden>
</>
);
};

IconLabel.propTypes = {
icon: PropTypes.object.isRequired,
isChecked: PropTypes.bool.isRequired,
};

const IconGrid = (props) => {
const { icons, selectedIcon, onChange } = props;

return (
<StyledIconGrid orientation="vertical" className="component-icon-picker__list">
{icons.map((icon) => {
const isChecked =
selectedIcon?.name === icon.name && selectedIcon?.iconSet === icon.iconSet;
const Label = memo(() => (
<>
<StyledIconButton
isSelected={isChecked}
key={icon.name}
name={icon.name}
iconSet={icon.iconSet}
/>
<VisuallyHidden>{icon.label}</VisuallyHidden>
</>
));
selectedIcon?.name === icon?.name && selectedIcon?.iconSet === icon?.iconSet;

return (
<CheckboxControl
key={icon.name}
label={<Label />}
label={<IconLabel isChecked={isChecked} icon={icon} />}
checked={isChecked}
onChange={() => onChange(icon)}
className="component-icon-picker__checkbox-control"
Expand Down
4 changes: 2 additions & 2 deletions components/icon-picker/inline-icon-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const InlineIconPicker = (props) => {
const { value, ...rest } = props;
const IconButton = useCallback(
({ onToggle }) => (
<Icon name={value.name} iconSet={value.iconSet} onClick={onToggle} {...rest} />
<Icon name={value?.name} iconSet={value?.iconSet} onClick={onToggle} {...rest} />
),
[value, rest],
);
Expand All @@ -46,7 +46,7 @@ export const IconPickerDropdown = (props) => {
<Dropdown
className="component-icon-picker-inline-button"
contentClassName="component-icon-picker-inline__content"
position="bottom right"
popoverProps={{ placement: 'bottom-start' }}
renderToggle={renderToggle}
renderContent={() => <StyledIconPickerDropdown {...iconPickerProps} />}
/>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "1.15.10",
"version": "1.15.11-alpha.2",
"description": "10up Components built for the WordPress Block Editor.",
"main": "./dist/index.js",
"source": "index.js",
Expand Down