npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aria-label-prop-type

v1.1.2

Published

A prop type for aria-label and accessibilityLabel that accepts either a string, or a string child somewhere in children

Downloads

1,971

Readme

aria-label-prop-type

A prop-type for aria-label and accessibilityLabel that accepts either a string, or a string child somewhere in children.

About

To have a screen-reader-accessible app/site, it's important for interactive elements to have a screen-reader-visible description.

Sometimes, that's in the form of an explicit aria-label or accessibilityLabel prop, for example in <button aria-label="Add a photo"><img src="addPhoto.png" /></button>.

Sometimes, the text within an element is enough, for example in <button>Done with questions</button>.

If you require an explicit aria-label prop in the second case, it becomes easier for the label to get out of sync with the screen text, making the app less usable. But in the first case, or other cases of a button built from non-text elements, having an aria-label is critical for using the app with a screen-reader.

That's where aria-label-prop-type comes in. aria-label-prop-type is a react prop-type for aria-label or accessibilityLabel or other label props, that enforces that a specified component must either have some string content somewhere within its children, or must have an explicit aria-label / accessibilityLabel prop.

Installation:

npm install aria-label-prop-type

While aria-label-prop-type should be installed as a dependency rather than a devDependency, it will check for process.env.NODE_ENV to reduce bundle size in production.

Basic Usage

For an interactive element that accepts children and an aria-label prop:

import * as React from 'react';
import * as PropTypes from 'prop-types';
import ariaLabelPropType from 'aria-label-prop-type';

// A simple, styled button
function ConfirmButton({children, ...props}) {
  return (
    <button
      style={{ backgroundColor: 'green', borderRadius: 12, padding: 8 }}
      {...props}
    >
      {children}
    </button>
  )
}

ConfirmButton.propTypes = {
  children: PropTypes.node,
  onPress: PropTypes.func,
  'aria-label': ariaLabelPropType.isRequired,
};

Render Prop Usage

For a component that (may) accept a render prop as a child:

import * as React from 'react';
import * as PropTypes from 'prop-types';
import ariaLabelPropType from 'aria-label-prop-type';

// A button that can change its contents based on its state:
function StatusButton({children, ...props}) {

  const [status, setStatus] = React.useState('inactive');

  const onMouseDown = React.useCallback(
    () => setStatus('active'),
    [setStatus]
  );
  const onMouseUp = React.useCallback(
    () => setStatus('inactive'),
    [setStatus]
  );

  // Accepts either child nodes, or a render prop that takes the current status:
  const content = typeof children === 'function' ? children(status) : children;

  return (
    <button
      {...props}
      onMouseDown={onMouseDown}
      onMouseUp={onMouseUp}
    >
      {content}
    </button>
  )
}

StatusButton.propTypes = {
  children: PropTypes.node,
  onPress: PropTypes.func,
  'aria-label': ariaLabelPropType.renderPropDefault('inactive').isRequired,
};

ReactNative usage

import * as React from 'react';
import * as PropTypes from 'prop-types';
import ariaLabelPropType from 'aria-label-prop-type';

// A simple, styled button
function ConfirmButton({children, onPress, accessibilityLabel, ...props}) {
  return (
    <TouchableOpacity onPress={onPress}>
      <View
        style={{ backgroundColor: 'green', borderRadius: 12, padding: 8 }}
        accessibilityLabel={accessibilityLabel}
        {...props}
      >
        {children}
      </View>
    </TouchableOpacity>
  )
}

ConfirmButton.propTypes = {
  children: PropTypes.node,
  onPress: PropTypes.func,
  accessibilityLabel: ariaLabelPropType.isRequired,
};

API

ariaLabelPropType is a prop-type describing a required either:

  1. string
  2. text (string) child somewhere within its children

ariaLabelPropType.isRequired is an alias for ariaLabelPropType, provided for convenience.

If your aria-label is optional (which I recommend it not being if you're focusing on accessibility), you can use PropTypes.string instead.

ariaLabelPropType.renderPropDefault(...defaultParams) is a wrapper around ariaLabelPropType for components that do or may accept a render prop as children. ariaLabelPropType will call the render prop with ...defaultParams, and check the resulting children nodes for text (if a string label is not present).

ariaLabelPropType.renderPropDefault(...defaultParams).isRequired is an alias for ariaLabelPropType.renderPropDefault(...defaultParams).