Skip to content

Added keyboard support for DxcToast #2233

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
77 changes: 63 additions & 14 deletions packages/lib/src/toast/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, useContext, useState } from "react";
import { memo, useContext, useState, useRef, useEffect } from "react";
import styled, { keyframes } from "styled-components";
import DxcActionIcon from "../action-icon/ActionIcon";
import DxcButton from "../button/Button";
Expand Down Expand Up @@ -51,6 +51,10 @@ const Toast = styled.output<{ semantic: ToastPropsType["semantic"]; isClosing: b
@media (max-width: ${responsiveSizes.medium}rem) {
max-width: 100%;
}

&:focus {
outline: none;
}
`;

const ContentContainer = styled.div<{ loading: ToastPropsType["loading"]; semantic: ToastPropsType["semantic"] }>`
Expand Down Expand Up @@ -87,24 +91,69 @@ const DxcToast = ({
semantic,
}: ToastPropsType) => {
const [isClosing, setIsClosing] = useState(false);
const toastRef = useRef<HTMLOutputElement>(null);
const previouslyFocusedElement = useRef<HTMLElement | null>(null);
const translatedLabels = useContext(HalstackLanguageContext);

const clearClosingAnimationTimer = useTimeout(
() => {
setIsClosing(true);
},
loading ? undefined : duration - 300
);
// Timeouts
const clearClosingAnimationTimer = useTimeout(() => setIsClosing(true), loading ? undefined : duration - 300);

const clearTimer = useTimeout(
() => {
onClear();
},
loading ? undefined : duration
);
const clearTimer = useTimeout(() => onClear(), loading ? undefined : duration);

useEffect(() => {
previouslyFocusedElement.current = document.activeElement as HTMLElement;

toastRef.current?.focus();

return () => {
previouslyFocusedElement.current?.focus?.();
};
}, []);

const handleOnKeyDown = (event: React.KeyboardEvent<HTMLOutputElement>) => {
if (event.key === "Tab") {
event.preventDefault();

const focusableElements = toastRef.current?.querySelectorAll<HTMLElement>(
'button, [tabindex]:not([tabindex="-1"])'
);
if (!focusableElements || focusableElements.length === 0) return;

const firstElement = focusableElements?.[0];
const lastElement = focusableElements?.[focusableElements.length - 1];
const activeElement = document.activeElement;

const elementsArray = Array.from(focusableElements);

if (!event.shiftKey) {
if (activeElement === lastElement) {
previouslyFocusedElement.current?.focus?.();
} else {
const currentIndex = elementsArray.indexOf(activeElement as HTMLElement);
const nextElement = focusableElements[currentIndex + 1];
nextElement?.focus();
}
} else {
if (activeElement === firstElement) {
previouslyFocusedElement.current?.focus?.();
} else {
const currentIndex = elementsArray.indexOf(activeElement as HTMLElement);
const prevElement = focusableElements[currentIndex - 1];
prevElement?.focus();
}
}
}
};

return (
<Toast isClosing={isClosing} role="status" semantic={semantic}>
<Toast
onKeyDown={handleOnKeyDown}
isClosing={isClosing}
role="status"
semantic={semantic}
tabIndex={-1}
ref={toastRef}
>
<ContentContainer loading={loading} semantic={semantic}>
<ToastIcon hideSemanticIcon={hideSemanticIcon} icon={icon} loading={loading} semantic={semantic} />
<Message>{message}</Message>
Expand Down