Skip to content

Instantly share code, notes, and snippets.

@JLarky
Last active March 26, 2023 18:08
Show Gist options
  • Save JLarky/9976b9fcb8f2aeaba9b8e3e14cee82db to your computer and use it in GitHub Desktop.
Save JLarky/9976b9fcb8f2aeaba9b8e3e14cee82db to your computer and use it in GitHub Desktop.
Asking the same question different AI models
Rewrite this component to be accessible:
export const Checkbox = ({
atom,
children,
}: {
atom: BooleanConfigAtom;
children: React.ReactNode;
}) => {
const [value, setValue] = useAtom(atom);
return (
<div
className="text-blue-500 flex items-center gap-3 cursor-pointer"
onClick={() => setValue(x => !x)}
>
{value ? <CheckboxOff className="w-5" /> : <CheckboxOn className="w-5" />}
<span className="text-black">{children}</span>
</div>
);
};
<div
className="text-blue-500 flex items-center gap-3 cursor-pointer"
onClick={() => setValue(x => !x)}
aria-role="checkbox"
aria-label={children}
>
{value ? <CheckboxOff className="w-5" /> : <CheckboxOn className="w-5" />}
</div>
export const Checkbox = ({
atom,
children,
}: {
atom: BooleanConfigAtom;
children: React.ReactNode;
}) => {
const [value, setValue] = useAtom(atom);
const id = useId();
return (
<div className="text-blue-500 flex items-center gap-3 cursor-pointer">
<label htmlFor={id}>
<input
id={id}
type="checkbox"
checked={value}
onChange={() => setValue(x => !x)}
className="sr-only"
/>
{value ? <CheckboxOff className="w-5" /> : <CheckboxOn className="w-5" />}
<span className="text-black">{children}</span>
</label>
</div>
);
};
import { useState } from 'react';
type CheckboxProps = {
id: string;
label: string;
defaultChecked?: boolean;
onChange: (checked: boolean) => void;
};
export const Checkbox = ({
id,
label,
defaultChecked = false,
onChange,
}: CheckboxProps) => {
const [checked, setChecked] = useState(defaultChecked);
const handleClick = () => {
const newChecked = !checked;
setChecked(newChecked);
onChange(newChecked);
};
const handleKeyDown = (event: React.KeyboardEvent) => {
if (event.key === 'Enter' || event.key === ' ') {
handleClick();
}
};
return (
<div role="checkbox" aria-checked={checked} tabIndex={0} onKeyDown={handleKeyDown}>
<input type="checkbox" id={id} checked={checked} onChange={() => {}} />
<label htmlFor={id} onClick={handleClick}>
{label}
</label>
</div>
);
};
import React from 'react';
import { useAtom } from 'jotai';
import { BooleanConfigAtom } from 'path/to/BooleanConfigAtom';
export const Checkbox = ({
atom,
children,
id,
labelText,
}: {
atom: BooleanConfigAtom;
children: React.ReactNode;
id: string;
labelText: string;
}) => {
const [value, setValue] = useAtom(atom);
const handleKeyPress = (event: React.KeyboardEvent<HTMLLabelElement>) => {
if (event.key === 'Enter' || event.key === ' ') {
setValue(x => !x);
event.preventDefault();
}
};
return (
<div className="text-blue-500 flex items-center gap-3">
<input
type="checkbox"
id={id}
checked={value}
onChange={() => setValue(x => !x)}
className="hidden"
/>
<label
htmlFor={id}
tabIndex={0}
className="cursor-pointer flex items-center gap-3"
onKeyPress={handleKeyPress}
aria-label={labelText}
>
{value ? (
<CheckboxOff className="w-5" aria-hidden="true" />
) : (
<CheckboxOn className="w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment