@tdcerhverv/switch
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Switch component

Usage

Import like this:

import { Switch } from '@tdcerhverv/switch';

Use like this:

<Switch label={labelText} />

or if you need html tags in the label

<Switch>
  Activate for <strong>double bacon</strong>!!
</Switch>

Usage Example

Here's an example of a realistic use case.

function DarkMode() {
  const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)')
    .matches;
  const [isOn, setIsOn] = useState(prefersDarkMode);

  return (
    <Switch label="Dark Mode" active={isOn} onChange={() => setIsOn(!isOn)} />
  );
}

Props

The Switch component extends general HTMLInputElement type, and supports, apart from type, all standard properties plus some more.

export interface ISwitch
  extends Omit<
    React.DetailedHTMLProps<
      React.InputHTMLAttributes<HTMLInputElement>,
      HTMLInputElement
    >,
    'type'
  > {
  label?: string;
  labelRequired?: string;
  active?: boolean;
  error?: boolean | string;
}

When a switch is required, you must supply a value labelRequired as well.

Known issues & To-do's

  • Improve tests
  • Add "passive" state
    • e.g. disable interaction while waiting for a request to complete.
  • Add interface support for ref
  • Improve HTML5 validation support

Readme

Keywords

none

Package Sidebar

Install

npm i @tdcerhverv/switch

Weekly Downloads

4

Version

1.2.1

License

Apache-2.0

Unpacked Size

39.3 kB

Total Files

10

Last publish

Collaborators

  • antongb
  • dev_jonas
  • jeppebenjaminsen
  • ahhah
  • abdok
  • mikkv
  • clausr1990
  • tdc-sigbe
  • mdige
  • nuudaybot
  • tdc-olila
  • jeroen.dk.nl
  • renand
  • stegr
  • m84100
  • m92212
  • m87979
  • vertic
  • rasladev
  • muddz
  • tajsonik
  • linnt
  • molg