Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useTabIndex provider and hook #35

Open
bashleigh opened this issue Feb 28, 2024 · 3 comments
Open

useTabIndex provider and hook #35

bashleigh opened this issue Feb 28, 2024 · 3 comments
Assignees
Labels
feature The issue relates to a new feature

Comments

@bashleigh
Copy link
Contributor

bashleigh commented Feb 28, 2024

Create a provider and hook to return a referenced sequential number to be used within aria label tab index.

Use the useRef method to create a reference to be updated within a function returned from the useTabIndex hook.

Something like so

export const useTabIndex = () => {
  const tabIndexRef = useRef(0)

  const tabIndex = () => {
    tabIndexValue = tabIndexRef.value
    tabIndexRef.value++

    return tabIndexValue
  }

  return { tabIndex }
}

This would then be usable within a component like so

import { useTabIndex } from '@reapit/elements'

export const MyLinkComponent = () => {
  const { tabIndex } = useTabIndex()

  return (<a tabindex={tabIndex()}>my link</a>)
}

A provider wrapper would be preferably not desired but if it can't be done without one then use one

Above approach will not work for the entire project. It'll have to be done using local storage

import { useState, useEffect } from 'react';
 
function useUniqueNumber() {
  const [number, setNumber] = useState(() => {
    // Get the current number from localStorage
    const currentNumber = Number(localStorage.getItem('uniqueNumber') || 0);
    // Increment the number
    const newNumber = currentNumber + 1;
    // Store the new number in localStorage
    localStorage.setItem('uniqueNumber', newNumber.toString());
    // Return the new number
    return newNumber;
  });
 
  useEffect(() => {
    // Update the number in localStorage when the number state changes
    localStorage.setItem('uniqueNumber', number.toString());
  }, [number]);
 
  return number;
}
@bashleigh bashleigh added the feature The issue relates to a new feature label Feb 28, 2024
@bashleigh bashleigh self-assigned this Feb 28, 2024
@bashleigh bashleigh moved this to To Review in Elements Roadmap Feb 28, 2024
@willmcvay
Copy link
Contributor

@bashleigh Don't think this is going to work - AXE standards do not permit a tab index greater than 0 - see https://dequeuniversity.com/rules/axe/4.8/tabindex?application=axeAPI

Any thoughts or should we close?

@adrian-reapit
Copy link
Contributor

I would avoid numbered tab index. It shouldn't be necessary in our type of sites

@kurtdoherty
Copy link
Contributor

I agree. Similar to the AXE standards, MDN docs suggest tabindex="0" and tabindex="-1" are the only values that should be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature The issue relates to a new feature
Projects
Status: To Review
Development

No branches or pull requests

4 participants