Skip to content
Open
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
25 changes: 22 additions & 3 deletions packages/components/src/checkbox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function CheckboxControl(
indeterminate,
help,
id: idProp,
disabled,
onChange,
...additionalProps
} = props;
Expand Down Expand Up @@ -89,8 +90,13 @@ export function CheckboxControl(
'inspector-checkbox-control',
idProp
);
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) => {
if ( disabled ) {
return;
}

onChange( event.target.checked );
};

return (
<BaseControl
Expand All @@ -108,14 +114,22 @@ export function CheckboxControl(
className={ clsx( 'components-checkbox-control', className ) }
>
<HStack spacing={ 0 } justify="start" alignment="top">
<span className="components-checkbox-control__input-container">
<span
className={ clsx(
'components-checkbox-control__input-container',
{
'is-disabled': disabled,
}
) }
>
<input
ref={ ref }
id={ id }
className="components-checkbox-control__input"
type="checkbox"
value="1"
onChange={ onChangeValue }
aria-disabled={ disabled }
checked={ checked }
aria-describedby={ !! help ? id + '__help' : undefined }
{ ...additionalProps }
Expand All @@ -137,7 +151,12 @@ export function CheckboxControl(
</span>
{ label && (
<label
className="components-checkbox-control__label"
className={ clsx(
'components-checkbox-control__label',
{
'is-disabled': disabled,
}
) }
htmlFor={ id }
>
{ label }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const meta: Meta< typeof CheckboxControl > = {
control: false,
},
help: { control: { type: 'text' } },
disabled: { control: { type: 'boolean' } },
},
parameters: {
controls: {
Expand Down
10 changes: 9 additions & 1 deletion packages/components/src/checkbox-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
.components-checkbox-control__label {
// Ensure label is aligned with checkbox along X axis
line-height: var(--checkbox-input-size);
cursor: pointer;

&:not(.is-disabled) {
cursor: pointer;
}
}

.components-checkbox-control__input[type="checkbox"] {
Expand Down Expand Up @@ -66,6 +69,11 @@
aspect-ratio: 1;
line-height: 1; // maintains proper height even with WP common.css
flex-shrink: 0;

// Disabled state:
&.is-disabled {
opacity: 0.3;
}
}

svg.components-checkbox-control__checked,
Expand Down
5 changes: 5 additions & 0 deletions packages/components/src/checkbox-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export type CheckboxControlProps = Pick<
* @deprecated
*/
heading?: ReactNode;
/**
* If disabled is true the checkbox will be disabled and apply the appropriate
* styles.
*/
disabled?: boolean;
};
Loading