5.24.1 • Published 10 months ago

@purpurds/checkbox v5.24.1

Weekly downloads
-
License
AGPL-3.0-only
Repository
-
Last release
10 months ago

import { Meta, ArgTypes, Primary, Subtitle } from "@storybook/blocks";

import * as CheckboxStories from "./src/checkbox.stories"; import packageInfo from "./package.json";

Checkbox

Version {packageInfo.version}

Showcase

Properties

Installation

Via NPM

Add the dependency to your consumer app like "@purpurds/purpur": "^x.y.z"

In MyApp.tsx

import "@purpurds/purpur/styles";

Examples

In MyComponent.tsx

Controlled.


For when you have to controll and use the state of the checkbox.

import { Checkbox } from "@purpurds/purpur";

export const MyComponent = () => {
  const [checked, setChecked] = useState(false);
  return (
    <div>
      <Checkbox
        id="my-checkbox"
        checked={checked}
        onChange={setChecked}
        label="My checkbox"
        labelPosition="right"
      />
    </div>
  );
};

Indeterminate.


When using the Indeterminate state, the toggle must be controlled.

import { Checkbox } from "@purpurds/purpur";

export const MyComponent = () => {
  const [checked, setChecked] = useState(false);

  return (
    <div>
      <Checkbox
        id="my-checkbox"
        checked={checked}
        onChange={setChecked}
        label="My checkbox"
        labelPosition="right"
      />
      <Button
        variant="primary"
        onClick={() =>
          setChecked((prevChecked) => (prevChecked === "indeterminate" ? false : "indeterminate"))
        }
      >
        Toggle indeterminate
      </Button>
    </div>
  );
};

Uncontrolled


For when you don't have to controll state of the checkbox, e.g. when in a form.

import { Checkbox } from "@purpurds/purpur";

export const MyComponent = () => {
  /**
   * The checkbox will render checked, and handle it's state itself.
   *
   * Since it is rendered in a form, it will render a checkbox input under the hood
   * that reflects its value and state.
   */
  return (
    <form>
      <Checkbox id="my-checkbox" defaultChecked label="My uncontrolled checkbox" />
    </form>
  );
};

With custom label (not recommended).


Use the aria-labelledby property and pass the id of the label.

import { Checkbox } from "@purpurds/purpur";

export const MyComponent = () => {
  return (
    <div>
      <label id="my-custom-label" htmlFor="my-checkbox">
        Custom label
      </label>
      <Checkbox aria-labeledby="my-custom-label" id="my-checkbox" {...otherProps} />
    </div>
  );
};

Without label (not recommended).


If there should be no label at all, use the aria-label to label the checkbox for screen readers.

import { Checkbox } from "@purpurds/purpur";

export const MyComponent = () => {
  return (
    <div>
      <Checkbox aria-label="checkbox some awesome stuff!" id="my-checkbox" {...otherProps} />
    </div>
  );
};
5.24.1

10 months ago

5.24.0

10 months ago

5.23.0

10 months ago

5.21.1

10 months ago

5.21.0

10 months ago

5.22.0

10 months ago

5.18.3

11 months ago

5.18.2

11 months ago

5.18.1

11 months ago

5.18.0

11 months ago

5.19.1

11 months ago

5.19.0

11 months ago

5.17.0

11 months ago

5.20.1

11 months ago

5.20.0

11 months ago

5.16.1

11 months ago

5.16.0

11 months ago

5.15.1

11 months ago

5.15.0

11 months ago

5.14.0

12 months ago

5.5.0

1 year ago

5.3.0

1 year ago

5.1.2

1 year ago

5.13.0

12 months ago

5.11.2

1 year ago

5.11.1

1 year ago

5.11.0

1 year ago

5.8.2

1 year ago

5.8.1

1 year ago

5.8.0

1 year ago

5.4.3

1 year ago

5.6.0

1 year ago

5.4.2

1 year ago

5.4.1

1 year ago

5.4.0

1 year ago

5.2.0

1 year ago

5.12.0

1 year ago

5.10.1

1 year ago

5.10.0

1 year ago

5.9.0

1 year ago

5.7.0

1 year ago

5.1.1

1 year ago

5.1.0

1 year ago

5.0.0

1 year ago

4.5.0

1 year ago

4.6.0

1 year ago

4.5.1

1 year ago

4.4.0

1 year ago

4.3.0

1 year ago

4.2.0

1 year ago

4.1.1

1 year ago

4.1.0

1 year ago

4.0.2

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

3.7.1

1 year ago

3.7.0

1 year ago

3.6.0

1 year ago

3.5.1

1 year ago

3.5.0

1 year ago

3.4.0

1 year ago

3.3.2

1 year ago

3.3.1

1 year ago

3.3.0

1 year ago

3.2.0

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.8.0

1 year ago