Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ngheadlessui/radio

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngheadlessui/radio

Radio Groups give you the same functionality as native HTML radio inputs, without any of the styling. They're perfect for building out custom UIs for selectors.

  • 0.0.2
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

ui-radio

Radio Groups give you the same functionality as native HTML radio inputs, without any of the styling. They're perfect for building out custom UIs for selectors.

Demo


Installation

# npm
npm install @ngheadlessui/radio

# Yarn
yarn add @ngheadlessui/radio

Basic Example

Radio Groups are built using the headless-tab-group, and headlessRadioOption components.

Clicking an option will select it, and when the Radio Group is focused, the arrow keys will change the selected option.

<headless-radio-group>
  <!-- You can use the headlessRadioOption as a component -->
  <headlessRadioOption>Option 1</headlessRadioOption>
  <headlessRadioOption>Option 2</headlessRadioOption>
  <headlessRadioOption>Option 3</headlessRadioOption>

  <!--or as a directive -->
  <div headlessRadioOption>Option 1</div>
  <div headlessRadioOption>Option 2</div>
  <div headlessRadioOption>Option 3</div>
</headless-radio-group>

Styling the checked radio item

This is a headless component so there are no styles included by default. Instead, the components expose useful information via bound classes that you can use to apply styles yourself.

To style the checked Radio, use the selected class checked-headless-radio, which tells you which radio is currently checked. For flexiblity.

.checked-headless-radio {
  @apply bg-sky-900 bg-opacity-75 text-white;
}

Disabling a radio item

To disable a radio, use the disabled input property on the headlessRadioOption component. Disabled radio items cannot be selected with the mouse.

<headless-tab-group>
  <headless-radio-group>
    <!-- You can use the headlessRadioOption as a component -->
    <headlessRadioOption [disabled]="true">Option 1</headlessRadioOption>
    <headlessRadioOption>Option 2</headlessRadioOption>
    <headlessRadioOption>Option 3</headlessRadioOption>

    <!--or as a directive -->
    <ng-container *ngFor="let plan of plans">
      <div headlessRadioOption [disabled]="plan.name === 'monthly'">
        {{ plan.name }}
      </div>
    </ng-container>
  </headless-radio-group>
</headless-tab-group>

To style disabled radio, use the disabled class disabled-headless-radio, which tells you whether or not that radio option is currently disabled.

.disabled-headless-radio {
  @apply text-red-300 bg-yellow-700 hover:bg-white/[0.12] hover:text-white;
}

Specifying the default checked radio

To select a radio is checked by default, use the checkedOptionIndex="number" property on the headless-tab-group component.

<headless-radio-group [checkedOptionIndex]="1">
  <headlessRadioOption>Option 1</headlessRadioOption>
  <headlessRadioOption>Option 2</headlessRadioOption>
  <headlessRadioOption>Option 3</headlessRadioOption>
</headless-radio-group>

Listening for changes

To run a function whenever the checked radio changes, listen to emissions from the (changeRadio) EventEmitter on the headless-radio-group component. (changeRadio) emits the index ($event) of the checked radio.

<headless-radio-group (changeRadio)="listenToChangeInRadio($event)">
  <headlessRadioOption>Option 1</headlessRadioOption>
  <headlessRadioOption>Option 2</headlessRadioOption>
  <headlessRadioOption>Option 3</headlessRadioOption>
</headless-radio-group>

Component APIs

headless-radio-group

The main Radio Group component.

PropsDefaultDescription
checkedOptionIndex0Number
The index of checked radio
(changeRadio)EventEmitter
This is fired when there is a change in checked radio

headlessRadioOption

The Radio component.

PropsDefaultDescription
idundefinedString
unique identifier of the radio option
disabledfalseBoolean
Whether or not selected of the Rasio option is currently disabled.

FAQs

Package last updated on 22 Jan 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc