Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 35 additions & 77 deletions src/A11y.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ export const A11y: React.FC<Props> = ({
a11yElStyle
);

const [a11yState, setA11yState] = useState({
hovered: false,
focused: false,
pressed: startPressed ? startPressed : false,
});
const [a11yStateHovered, setA11yStateHovered] = useState(false);
const [a11yStateFocused, setA11yStateFocused] = useState(false);
const [a11yStatePressed, setA11yStatePressed] = useState(startPressed);

const a11yScreenReader = useAnnounceStore((state) => state.a11yScreenReader);

Expand Down Expand Up @@ -147,11 +145,7 @@ export const A11y: React.FC<Props> = ({
if (role !== 'content' && role !== 'image' && !disabled) {
domElement.style.cursor = 'pointer';
}
setA11yState({
hovered: true,
focused: a11yState.focused,
pressed: a11yState.pressed,
});
setA11yStateHovered(true);
}
};
// @ts-ignore
Expand All @@ -164,11 +158,7 @@ export const A11y: React.FC<Props> = ({
if (!overHtml.current && !overMesh.current) {
if (componentIsMounted.current) {
domElement.style.cursor = 'default';
setA11yState({
hovered: false,
focused: a11yState.focused,
pressed: a11yState.pressed,
});
setA11yStateHovered(false);
}
}
};
Expand All @@ -183,17 +173,13 @@ export const A11y: React.FC<Props> = ({
}

function handleToggleBtnClick() {
if (a11yState.pressed) {
if (a11yStatePressed) {
if (typeof deactivationMsg === 'string')
a11yScreenReader(deactivationMsg);
} else {
if (typeof activationMsg === 'string') a11yScreenReader(activationMsg);
}
setA11yState({
hovered: a11yState.hovered,
focused: a11yState.focused,
pressed: !a11yState.pressed,
});
setA11yStatePressed(!a11yStatePressed);
if (typeof actionCall === 'function') actionCall();
}

Expand All @@ -209,7 +195,7 @@ export const A11y: React.FC<Props> = ({
<button
r3f-a11y="true"
{...disabledBtnAttr}
aria-pressed={a11yState.pressed ? 'true' : 'false'}
aria-pressed={a11yStatePressed ? 'true' : 'false'}
tabIndex={tabIndex ? tabIndex : 0}
style={Object.assign(
constHiddenButScreenreadable,
Expand All @@ -220,6 +206,8 @@ export const A11y: React.FC<Props> = ({
)}
onPointerOver={handleOnPointerOver}
onPointerOut={handleOnPointerOut}
onTouchStart={handleOnPointerOver}
onTouchEnd={handleOnPointerOut}
onClick={(e) => {
e.stopPropagation();
if (disabled) {
Expand All @@ -229,18 +217,10 @@ export const A11y: React.FC<Props> = ({
}}
onFocus={() => {
if (typeof focusCall === 'function') focusCall();
setA11yState({
hovered: a11yState.hovered,
focused: true,
pressed: a11yState.pressed,
});
setA11yStateFocused(true);
}}
onBlur={() => {
setA11yState({
hovered: a11yState.hovered,
focused: false,
pressed: a11yState.pressed,
});
setA11yStateFocused(false);
}}
>
{description}
Expand All @@ -262,6 +242,8 @@ export const A11y: React.FC<Props> = ({
)}
onPointerOver={handleOnPointerOver}
onPointerOut={handleOnPointerOut}
onTouchStart={handleOnPointerOver}
onTouchEnd={handleOnPointerOut}
onClick={(e) => {
e.stopPropagation();
if (disabled) {
Expand All @@ -271,18 +253,10 @@ export const A11y: React.FC<Props> = ({
}}
onFocus={() => {
if (typeof focusCall === 'function') focusCall();
setA11yState({
hovered: a11yState.hovered,
focused: true,
pressed: a11yState.pressed,
});
setA11yStateFocused(true);
}}
onBlur={() => {
setA11yState({
hovered: a11yState.hovered,
focused: false,
pressed: a11yState.pressed,
});
setA11yStateFocused(false);
}}
>
{description}
Expand All @@ -302,25 +276,19 @@ export const A11y: React.FC<Props> = ({
href={href}
onPointerOver={handleOnPointerOver}
onPointerOut={handleOnPointerOut}
onTouchStart={handleOnPointerOver}
onTouchEnd={handleOnPointerOut}
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
if (typeof actionCall === 'function') actionCall();
}}
onFocus={() => {
if (typeof focusCall === 'function') focusCall();
setA11yState({
hovered: a11yState.hovered,
focused: true,
pressed: a11yState.pressed,
});
setA11yStateFocused(true);
}}
onBlur={() => {
setA11yState({
hovered: a11yState.hovered,
focused: false,
pressed: a11yState.pressed,
});
setA11yStateFocused(false);
}}
>
{description}
Expand All @@ -347,20 +315,14 @@ export const A11y: React.FC<Props> = ({
)}
onPointerOver={handleOnPointerOver}
onPointerOut={handleOnPointerOut}
onTouchStart={handleOnPointerOver}
onTouchEnd={handleOnPointerOut}
onBlur={() => {
setA11yState({
hovered: a11yState.hovered,
focused: false,
pressed: a11yState.pressed,
});
setA11yStateFocused(false);
}}
onFocus={() => {
if (typeof focusCall === 'function') focusCall();
setA11yState({
hovered: a11yState.hovered,
focused: true,
pressed: a11yState.pressed,
});
setA11yStateFocused(true);
}}
/>
);
Expand All @@ -378,20 +340,14 @@ export const A11y: React.FC<Props> = ({
)}
onPointerOver={handleOnPointerOver}
onPointerOut={handleOnPointerOut}
onTouchStart={handleOnPointerOver}
onTouchEnd={handleOnPointerOut}
onBlur={() => {
setA11yState({
hovered: a11yState.hovered,
focused: false,
pressed: a11yState.pressed,
});
setA11yStateFocused(false);
}}
onFocus={() => {
if (typeof focusCall === 'function') focusCall();
setA11yState({
hovered: a11yState.hovered,
focused: true,
pressed: a11yState.pressed,
});
setA11yStateFocused(true);
}}
>
{description}
Expand All @@ -403,7 +359,9 @@ export const A11y: React.FC<Props> = ({

const HtmlAccessibleElement = React.useMemo(returnHtmlA11yEl, [
description,
a11yState,
a11yStateHovered,
a11yStateFocused,
a11yStatePressed,
hidden,
tabIndex,
href,
Expand All @@ -415,7 +373,7 @@ export const A11y: React.FC<Props> = ({
]);

let AltText = null;
if (showAltText && a11yState.hovered) {
if (showAltText && a11yStateHovered) {
AltText = (
<div
aria-hidden={true}
Expand Down Expand Up @@ -453,9 +411,9 @@ export const A11y: React.FC<Props> = ({
return (
<A11yContext.Provider
value={{
hover: a11yState.hovered,
focus: a11yState.focused,
pressed: a11yState.pressed,
hover: a11yStateHovered,
focus: a11yStateFocused,
pressed: a11yStatePressed,
}}
>
<group
Expand Down