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

Feat: [OV-44110] Added checkbox completion style #443

Merged
merged 2 commits into from
Jun 20, 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
5 changes: 5 additions & 0 deletions .changeset/nervous-eyes-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@igloo-ui/checkbox': minor
---

Added a new appearance called 'completion' to the checkbox component.
110 changes: 75 additions & 35 deletions packages/Checkbox/src/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { Meta, StoryFn } from '@storybook/react';
import { Meta, StoryObj } from '@storybook/react';

import Section from '@components/section';
import readme from '../README.md';
Expand All @@ -22,41 +22,81 @@ export default {
},
} as Meta<typeof Checkbox>;

export const Overview = {

type Story = StoryObj<typeof Checkbox>;

export const Overview: Story = {
args: {
htmlFor: 'ids-checkbox',
children: 'Label',
},
children: 'Label'
}
};

export const Checked: Story = {
render: () => {
return (
<Section>
<Checkbox htmlFor="ids-checkbox-active" checked>
Label
</Checkbox>
</Section>
)
}
};

export const Checked: React.VFC<unknown> = () => (
<Checkbox htmlFor="ids-checkbox-active" checked>
Label
</Checkbox>
);

export const Indeterminate: React.VFC<unknown> = () => (
<Checkbox htmlFor="ids-checkbox-indeterminate" indeterminate>
Label
</Checkbox>
);

export const Disabled: React.VFC<unknown> = () => (
<Section>
<Checkbox htmlFor="ids-checkbox-disabled" disabled>
Label
</Checkbox>

<Checkbox htmlFor="ids-checkbox-disabled-active" checked disabled>
Label
</Checkbox>

<Checkbox
htmlFor="ids-checkbox-disabled-indeterminate"
indeterminate
disabled
>
Label
</Checkbox>
</Section>
);
export const Indeterminate: Story = {
render: () => {
return (
<Section>
<Checkbox htmlFor="ids-checkbox-indeterminate" indeterminate>
Label
</Checkbox>
</Section>
)
}
};

export const Disabled: Story = {
render: () => {
return (
<Section>
<Checkbox htmlFor="ids-checkbox-disabled" disabled>
Label
</Checkbox>

<Checkbox htmlFor="ids-checkbox-disabled-active" checked disabled>
Label
</Checkbox>

<Checkbox
htmlFor="ids-checkbox-disabled-indeterminate"
indeterminate
disabled
>
Label
</Checkbox>
</Section>
)
}
};

export const CompletionCheckbox: Story = {
render: () => {
return (
<Section>
<Checkbox htmlFor="ids-checkbox-completion-default" appearance="completion">
Label
</Checkbox>
<Checkbox htmlFor="ids-checkbox-completion-active" checked appearance="completion">
Label
</Checkbox>
<Checkbox htmlFor="ids-checkbox-completion-disabled" disabled appearance="completion">
Label
</Checkbox>
<Checkbox htmlFor="ids-checkbox-completion-disabled-active" checked disabled appearance="completion">
Label
</Checkbox>
</Section>
)
}
};
5 changes: 5 additions & 0 deletions packages/Checkbox/src/Checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ describe('Checkbox', () => {
const { asFragment } = setUp({ indeterminate: true });
expect(asFragment()).toMatchSnapshot();
});

test('It should render a completion appearance', () => {
setUp({ appearance: "completion" });
expect(screen.getByRole('checkbox')).toHaveClass('ids-checkbox--completion');
});
});
37 changes: 32 additions & 5 deletions packages/Checkbox/src/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import * as React from 'react';
import cx from 'classnames';

import Checkmark from '@igloo-ui/icons/dist/Checkmark';

import './checkbox.scss';

export type CheckboxAppearance = 'default' | 'completion';

export interface CheckboxProps extends React.ComponentPropsWithRef<'input'> {
/** The appearance of the checkbox */
appearance?: CheckboxAppearance;
/** The content to display inside the label */
children?: React.ReactNode;
/** Add a specific class to the checkbox */
Expand All @@ -25,6 +31,7 @@ export interface CheckboxProps extends React.ComponentPropsWithRef<'input'> {
const Checkbox: React.FunctionComponent<CheckboxProps> = React.forwardRef(
(
{
appearance = 'default',
children,
className,
dataTest,
Expand Down Expand Up @@ -64,22 +71,42 @@ const Checkbox: React.FunctionComponent<CheckboxProps> = React.forwardRef(
setStatus(!status);
};

const showLabel = appearance === 'completion' || children;

return (
<span className={cx('ids-form-control', className)}>
<span
className={cx('ids-form-control', className, {
[`ids-form-control--${appearance}`]: appearance !== 'default',
})}
>
<input
ref={checkRef}
id={htmlFor}
className="ids-checkbox"
className={cx('ids-checkbox', {
[`ids-checkbox--${appearance}`]: appearance !== 'default',
})}
data-test={dataTest}
checked={status}
disabled={disabled}
type="checkbox"
onChange={handleOnChange}
{...rest}
/>
{children && (
<label className="ids-checkbox__label" htmlFor={htmlFor}>
{children}
{showLabel && (
<label
className={cx('ids-checkbox__label', {
[`ids-checkbox__label--${appearance}`]: appearance !== 'default',
})}
htmlFor={htmlFor}
>
{appearance === 'completion' && (
<span className="ids-checkbox__box">
<Checkmark size="small" className="ids-checkbox__check" />
</span>
)}
{children && (
<span className="ids-checkbox__label-text">{children}</span>
)}
</label>
)}
</span>
Expand Down
134 changes: 132 additions & 2 deletions packages/Checkbox/src/checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
--ids-checkbox-font-family: #{tokens.$primary-font-family};
--ids-checkbox-font-weight: #{tokens.$font-weight-regular};
--ids-checkbox-font-size: #{tokens.$font-size-4};
--ids-checkbox-focus: #{tokens.$focus};
--ids-checkbox-size: #{tokens.$font-size-3};
--ids-checkbox-radius: #{tokens.$border-radius-sm};
--ids-checkbox-check: #{tokens.$samoyed};
Expand All @@ -21,6 +22,17 @@
--ids-checkbox-border-checked: #{tokens.$electric-blue-500};
--ids-checkbox-check-width: 4px;
--ids-checkbox-check-height: 8px;

/* Completion */
--ids-checkbox-completion-size: #{tokens.$space-4};
--ids-checkbox-completion-radius: 50%;
--ids-checkbox-completion-background-disabled: #{tokens.$grey-200};
--ids-checkbox-completion-border-disabled: #{tokens.$grey-300};
--ids-checkbox-completion-check-size: #{tokens.$space-3};
--ids-checkbox-completion-check-color-hover: #{tokens.$grey-800};
--ids-checkbox-completion-check-color-selected: #{tokens.$samoyed};
--ids-checkbox-completion-check-color-default: #{tokens.$grey-200};
--ids-checkbox-completion-check-color-disabled: #{tokens.$grey-400};
}

%check {
Expand Down Expand Up @@ -50,6 +62,14 @@
font-size: var(--ids-checkbox-font-size);
font-weight: var(--ids-checkbox-font-weight);
color: var(--ids-checkbox-text);

&--completion {
align-items: center;
display: flex;
}
}

.ids-checkbox__label-text {
padding-left: var(--ids-checkbox-margin);
}

Expand Down Expand Up @@ -87,7 +107,7 @@

&:focus {
outline: none;
box-shadow: var(--focus);
box-shadow: var(--ids-checkbox-focus);
}

&:hover,
Expand All @@ -114,6 +134,7 @@
&:checked::after {
@extend %check;

display: block;
left: 2px;
top: 7px;
animation: check-animation ease-out 0.25s 0.15s both;
Expand All @@ -136,7 +157,101 @@
}
}

.ids-checkbox:disabled + .ids-checkbox__label {
.ids-checkbox--completion {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
padding: 0;
border: 0;
height: 1px;
width: 1px;
overflow: hidden;
}

.ids-checkbox__box {
background: var(--ids-checkbox-background);
border: 1px solid var(--ids-checkbox-border);
border-radius: var(--ids-checkbox-completion-radius);
box-sizing: border-box;
color: var(--ids-checkbox-check);
cursor: pointer;
display: inline-block;
flex: 0 0 auto;
height: var(--ids-checkbox-completion-size);
margin: 0;
overflow: hidden;
position: relative;
transition: background 0.05s ease-in-out;
width: var(--ids-checkbox-completion-size);

.ids-checkbox--completion:checked ~ .ids-checkbox__label & {
border-color: var(--ids-checkbox-border-checked);
background: var(--ids-checkbox-background-checked);
}

.ids-checkbox--completion:disabled ~ .ids-checkbox__label & {
cursor: not-allowed;
border-color: var(--ids-checkbox-completion-border-disabled);
background: var(--ids-checkbox-completion-background-disabled);
}

.ids-checkbox--completion:focus ~ .ids-checkbox__label & {
outline: none;
box-shadow: var(--ids-checkbox-focus);
}

.ids-checkbox--completion:hover ~ .ids-checkbox__label &,
.ids-checkbox--completion:checked:hover ~ .ids-checkbox__label & {
border-color: var(--ids-checkbox-border-hover);
}

.ids-checkbox--completion:focus:not(:focus-visible) ~ .ids-checkbox__label & {
box-shadow: none;
}

.ids-checkbox--completion:disabled:hover ~ .ids-checkbox__label & {
border-color: var(--ids-checkbox-border);
}

.ids-checkbox--completion ~ .ids-checkbox__label & .ids-checkbox__check {
color: var(--ids-checkbox-completion-check-color-default);
font-size: 0;
height: var(--ids-checkbox-completion-check-size);
left: 50%;
position: absolute;
stroke-dasharray: 20;
stroke-dashoffset: 0;
top: 50%;
transform: translate(-50%, -50%);
width: var(--ids-checkbox-completion-check-size);
}

.ids-checkbox--completion:hover ~ .ids-checkbox__label & .ids-checkbox__check {
color: var(--ids-checkbox-completion-check-color-hover);
}

.ids-checkbox--completion:disabled ~ .ids-checkbox__label & .ids-checkbox__check {
display: none;
}

.ids-checkbox--completion:checked ~ .ids-checkbox__label & .ids-checkbox__check {
animation: check-completion-animation .30s ease-out .15s forwards;
color: var(--ids-checkbox-completion-check-color-selected);
stroke-dashoffset: 20;
}

.ids-checkbox--completion:checked:hover ~ .ids-checkbox__label & .ids-checkbox__check {
color: var(--ids-checkbox-completion-check-color-selected);
}

.ids-checkbox--completion:checked:disabled ~ .ids-checkbox__label & .ids-checkbox__check {
animation: none;
color: var(--ids-checkbox-completion-check-color-disabled);
display: block;
stroke-dashoffset: 0;
}
}

.ids-checkbox:disabled ~ .ids-checkbox__label {
color: var(--ids-checkbox-text-disabled);
}

Expand All @@ -159,3 +274,18 @@
height: var(--ids-checkbox-check-height);
}
}

@keyframes check-completion-animation {
0% {
opacity: 0;
stroke-dashoffset: 20;
}

30% {
opacity: 1;
}

100% {
stroke-dashoffset: 0;
}
}