• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atomicpages / pretty-checkbox-react / 9820775925

06 Jul 2024 04:56PM UTC coverage: 100.0%. Remained the same
9820775925

push

github

atomicpages
feat: support ESM and move to tsup

67 of 67 branches covered (100.0%)

Branch coverage included in aggregate %.

69 of 69 new or added lines in 15 files covered. (100.0%)

171 of 171 relevant lines covered (100.0%)

11.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/components/switch/useAriaChecked.ts
1
import * as React from "react";
2✔
2

3
import type { PCRSwitchProps } from "../../typings/PCRSwitchProps";
4

5
export type UseAriaCheckedOptions = {
6
  setState?: PCRSwitchProps["setState"];
7
  checked?: PCRSwitchProps["checked"];
8
};
9

10
const handler = (e: any) => {
2✔
11
  e.currentTarget.setAttribute("aria-checked", e.currentTarget.checked + "");
2✔
12
};
13

14
/**
15
 * A small hook to help manage correct aria-checked state when switch
16
 * is used as an uncontrolled component. We need this to run
17
 * for a11y purposes. FOr the `switch` role, `aria-checked` is required.
18
 */
19
export const useAriaChecked = ({
2✔
20
  setState,
21
  checked,
22
}: UseAriaCheckedOptions) => {
23
  const ref = React.useRef<HTMLInputElement>(null);
7✔
24

25
  React.useEffect(() => {
7✔
26
    const elem = ref.current;
7✔
27
    let bound = false;
7✔
28

29
    if (!setState && !checked && elem) {
7✔
30
      elem.setAttribute("aria-checked", elem.checked + "");
4✔
31
      elem.addEventListener("change", handler);
4✔
32
      bound = true;
4✔
33
    }
34

35
    return () => {
7✔
36
      if (bound && elem) {
7✔
37
        elem.removeEventListener("change", handler);
4✔
38
      }
39
    };
40
  }, [setState, checked]);
41

42
  return ref;
7✔
43
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc