Socket
Socket
Sign inDemoInstall

@accessible/use-conditional-focus

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@accessible/use-conditional-focus

A React hook for focusing elements and their children conditionally.


Version published
Weekly downloads
2.2K
increased by0.83%
Maintainers
1
Weekly downloads
 
Created
Source

useConditionalFocus()

Bundlephobia Code coverage Build status NPM Version MIT License

npm i @accessible/use-conditional-focus

A React hook that will focus elements conditionally. By default this will focus on the first focusable child of the provided root element, but you can optionally include the root as well.

Quick Start

import * as React from 'react'
import useConditionalFocus from '@accessible/use-conditional-focus'

const Component = () => {
  const ref = React.useRef(null)
  const [visible, setVisible] = React.useState(false)
  // Focuses the first focusable child in the `ref` element when
  // visible is `true`
  useConditionalFocus(ref, visible)

  return (
    <div>
      <div ref={ref}>
        // This button will be focused when `visible` is true
        <button onClick={() => setVisible(false)}>Close me</button>
      </div>
      <button onClick={() => setVisible(true)}>Click me</button>
    </div>
  )
}

API

useConditionalFocus(target, shouldFocus, options?)

Arguments
PropTypeDefaultRequired?Description
targetReact.RefObject<T> | T | Window | Document | nullYesA React ref, element, window, or document
shouldFocusbooleanfalseYesProvide a true value here to focus the first focusable child in the element.
optionsUseConditionalFocusOptions{includeRoot: false, preventScroll: false}NoSee UseConditionalFocusOptions.
UseConditonalFocusOptions
PropTypeDefaultRequired?Description
includeRootbooleanfalseNoWhen true this will try to focus on the root element in addition to its children.
preventScrollbooleanfalseNoWhen true this will prevent your browser from scrolling the document to bring the newly-focused element into view.
Returns void

LICENSE

MIT

Keywords

FAQs

Package last updated on 27 Nov 2020

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