Socket
Socket
Sign inDemoInstall

@accessible/radio

Package Overview
Dependencies
8
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @accessible/radio

🅰 An accessible and versatile radio component for React


Version published
Weekly downloads
226
decreased by-9.6%
Maintainers
1
Install size
167 kB
Created
Weekly downloads
 

Changelog

Source

4.0.0 (2020-07-11)

⚠ BREAKING CHANGES

  • Removes useControls(), useChecked(), useDisabled(), useFocused() hooks and adds more strict, correct types

  • improve types and api (e774af8)

Readme

Source

<Radio>

Bundlephobia Types Code coverage Build status NPM Version MIT License

npm i @accessible/radio

An accessible radio component for React. This library allows you to create your own a radio with your own styles while maintaining the ability to focus and update a radio input with the keyboard.

Quick Start

Check out the example on CodeSandbox

import {RadioGroup, Radio, Mark} from '@accessible/radio'

const MyRadio = () => (
  <RadioGroup name='favorite_food' defaultValue='pizza'>
    <h2>What is your favorite food?</h2>

    <label>
      <Radio value='pizza'>
        <span className='my-radio'>
          <Mark checkedClass='checked' uncheckedClass='unchecked'>
            <span className='mark' />
          </Mark>
        </span>
      </Radio>
      Pizza
    </label>

    <label>
      <Radio value='tacos'>
        <span className='my-radio'>
          <Mark checkedClass='checked' uncheckedClass='unchecked'>
            <span className='mark' />
          </Mark>
        </span>
      </Radio>
      Tacos
    </label>
  </RadioGroup>
)

API

<RadioGroup>

Creates context that controls the child <Radio> components. This is also where you set controlled values and default values for the radio group.

Props
PropTypeDefaultRequired?Description
namestringundefinedYesThe name of the checkbox group (applied to each child <Radio> input)
valuevalue: string | number | string[] | undefinedundefinedNoMakes the radio group a controlled component which can no longer be updated with the setValue control or any <Radio> controls. It should be the same as one of the values in the child <Radio> inputs.
defaultValuevalue: string | number | string[] | undefinedundefinedNoThis sets the default radio group value. It should be the same as one of the values in the child <Radio> inputs.
onChange(value: string | number | string[] | undefined) => anyundefinedNoThis callback fires each time the checked value changes
childrenReact.ReactNodeundefinedNoAny React nodes, including your Radio inputs

useRadioGroup()

A React hook that returns the RadioGroupContextValue for the nearest <RadioGroup> parent.

RadioGroupContextValue

interface RadioGroupContextValue {
  name: string
  value: string | number | string[] | undefined
  setValue: (
    value:
      | string``
      | number
      | string[]
      | undefined
      | ((
          current: string | number | string[] | undefined
        ) => string | number | string[] | undefined)
  ) => void
}

<Radio>

Creates a visually hidden radio input that is focusable and accessible via keyboard navigation. All props passed to this component are applied to the <input>. This also creates a context provider enabling the other components in this library to access the radio's state deep in the tree.

Props
PropTypeDefaultRequired?Description
disabledbooleanfalseNoDisables this radio option
onChange(checked: boolean) => anyundefinedNoCalled each time the checked state of this option changes.
childrenReact.ReactNodeundefinedNoYour custom styled radio.

<Mark>

A convenient component for conditionally adding class names and styles when the <Radio> component is checked/unchecked. It must be a child of a <Radio>.

Props
PropTypeDefaultRequired?Description
uncheckedClassstringundefinedNoThis class name will be applied to the child element when the radio is unchecked.
checkedClassstringundefinedNoThis class name will be applied to the child element when the radio is checked.
uncheckedStyleReact.CSSPropertiesundefinedNoThese styles will be applied to the child element when the radio is unchecked.
checkedStyleReact.CSSPropertiesundefinedNoThese styles name will be applied to the child element when the radio is checked.
childrenReact.ReactNodeundefinedYesThe child you wish to render when the radio is checked.

<Checked>

The child of this component will only render when the <Radio> is in a checked state. It must be a child of a <Radio>.

Props
PropTypeDefaultRequired?Description
childrenReact.ReactNodeundefinedYesThe child you wish to render when the radio is checked.

<Unchecked>

The child of this component will only render when the <Radio> is in an unchecked state. It must be a child of a <Radio>.

Props
PropTypeDefaultRequired?Description
childrenReact.ReactNodeundefinedYesThe child you wish to render when the radio is unchecked.

useRadio()

A React hook that returns the RadioContextValue for the nearest <Radio> parent.

RadioContextValue

interface RadioContextValue {
  // Does the radio have a `checked` property?
  checked: boolean
  // Is the radio currently focused?
  focused: boolean
  // Is the radio currently disabled?
  disabled: boolean
  // Checks the radio
  check: () => void
  // Unchecks the radio
  uncheck: () => void
}

LICENSE

MIT

Keywords

FAQs

Last updated on 11 Jul 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc