fix(SW-184): fixed implementation of useTrapFocus
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"use client"
|
||||
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { Lang, languages } from "@/constants/languages"
|
||||
import useDropdownStore from "@/stores/main-menu"
|
||||
|
||||
import { CheckIcon, ChevronLeftIcon } from "@/components/Icons"
|
||||
import Link from "@/components/TempDesignSystem/Link"
|
||||
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"
|
||||
import { useHandleKeyUp } from "@/hooks/useHandleKeyUp"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { useTrapFocus } from "@/hooks/useTrapFocus"
|
||||
|
||||
import styles from "./languageSwitcher.module.css"
|
||||
|
||||
import { LanguageSwitcherProps } from "@/types/components/header/languageSwitcher"
|
||||
|
||||
export default function LanguageSwitcherContent({
|
||||
urls,
|
||||
variant,
|
||||
}: LanguageSwitcherProps) {
|
||||
const intl = useIntl()
|
||||
const currentLanguage = useLang()
|
||||
const { isLanguageSwitcherOpen, toggleLanguageSwitcher } = useDropdownStore()
|
||||
const languageSwitcherRef = useTrapFocus()
|
||||
const urlKeys = Object.keys(urls) as Lang[]
|
||||
|
||||
useHandleKeyUp((event: KeyboardEvent) => {
|
||||
if (event.key === "Escape" && isLanguageSwitcherOpen) {
|
||||
toggleLanguageSwitcher()
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<div className={styles.content} ref={languageSwitcherRef}>
|
||||
{variant === "mobileHeader" ? (
|
||||
<div className={styles.backWrapper}>
|
||||
<button
|
||||
type="button"
|
||||
className={styles.backButton}
|
||||
onClick={toggleLanguageSwitcher}
|
||||
>
|
||||
<ChevronLeftIcon color="red" />
|
||||
<Subtitle type="one">Main Menu</Subtitle>
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div className={styles.languageWrapper}>
|
||||
<Subtitle className={styles.subtitle} type="two">
|
||||
{intl.formatMessage({ id: "Select your language" })}
|
||||
</Subtitle>
|
||||
<ul className={styles.list}>
|
||||
{urlKeys.map((key) => {
|
||||
const url = urls[key]?.url
|
||||
const isActive = currentLanguage === key
|
||||
if (url) {
|
||||
return (
|
||||
<li key={key}>
|
||||
<Link
|
||||
className={`${styles.link} ${isActive ? styles.active : ""}`}
|
||||
color="burgundy"
|
||||
href={url}
|
||||
>
|
||||
{languages[key]}
|
||||
{isActive ? <CheckIcon color="burgundy" /> : null}
|
||||
</Link>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
@keyframes slide-in {
|
||||
from {
|
||||
right: -100vw;
|
||||
}
|
||||
|
||||
to {
|
||||
right: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.button {
|
||||
background-color: transparent;
|
||||
color: var(--Base-Text-High-contrast);
|
||||
font-family: var(--typography-Caption-Regular-fontFamily);
|
||||
font-size: var(--typography-Caption-Regular-fontSize);
|
||||
border-width: 0;
|
||||
padding: 0;
|
||||
cursor: pointer;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, max-content) 1fr;
|
||||
gap: var(--Spacing-x1);
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.chevron {
|
||||
justify-self: end;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.chevron.isExpanded {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
|
||||
.dropdown {
|
||||
position: fixed;
|
||||
top: var(--main-menu-mobile-height);
|
||||
right: -100vw;
|
||||
bottom: 0;
|
||||
width: 100%;
|
||||
background-color: var(--Base-Surface-Primary-light-Normal);
|
||||
transition: right 0.3s;
|
||||
}
|
||||
|
||||
.dropdown.isExpanded {
|
||||
display: block;
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.backWrapper {
|
||||
background-color: var(--Base-Surface-Secondary-light-Normal);
|
||||
padding: var(--Spacing-x2);
|
||||
}
|
||||
|
||||
.backButton {
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
color: var(--Base-Text-High-contrast);
|
||||
font-family: var(--typography-Subtitle-1-fontFamily);
|
||||
font-weight: var(--typography-Subtitle-1-fontWeight);
|
||||
font-size: var(--typography-Subtitle-1-Mobile-fontSize);
|
||||
padding: 0;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--Spacing-x1);
|
||||
}
|
||||
|
||||
.languageWrapper {
|
||||
display: grid;
|
||||
gap: var(--Spacing-x3);
|
||||
padding: var(--Spacing-x3) var(--Spacing-x2);
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
font-family: var(--typography-Subtitle-2-fontFamily);
|
||||
font-size: var(--typography-Subtitle-2-Mobile-fontSize);
|
||||
font-weight: var(--typography-Subtitle-2-fontWeight);
|
||||
color: var(--Base-Text-High-contrast, #4d001b);
|
||||
}
|
||||
|
||||
.list {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.link {
|
||||
color: var(--Scandic-Brand-Burgundy);
|
||||
font-family: var(--typography-Body-Regular-fontFamily);
|
||||
font-size: var(--typography-Body-Regular-fontSize);
|
||||
line-height: var(--typography-Body-Regular-lineHeight);
|
||||
letter-spacing: var(--typography-Body-Regular-letterSpacing);
|
||||
padding: var(--Spacing-x1);
|
||||
border-radius: var(--Corner-radius-Medium);
|
||||
display: flex;
|
||||
gap: var(--Spacing-x1);
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
text-decoration: none;
|
||||
border-radius: var(--Corner-radius-Medium);
|
||||
}
|
||||
|
||||
.link.active,
|
||||
.link:hover {
|
||||
background-color: var(--Base-Surface-Primary-light-Hover-alt);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
@media screen and (min-width: 768px) {
|
||||
.languageSwitcher {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.backWrapper,
|
||||
.backButton {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.languageWrapper {
|
||||
padding: var(--Spacing-x2) var(--Spacing-x3);
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.dropdown {
|
||||
position: absolute;
|
||||
top: 2.25rem;
|
||||
background-color: var(--Base-Surface-Primary-light-Normal);
|
||||
border-radius: var(--Corner-radius-Large);
|
||||
box-shadow: 0px 0px 14px 6px #0000001a;
|
||||
display: none;
|
||||
min-width: 12.5rem;
|
||||
z-index: 1;
|
||||
bottom: auto;
|
||||
}
|
||||
|
||||
/* Triangle above dropdown */
|
||||
.dropdown::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: -1.25rem;
|
||||
right: 2.4rem;
|
||||
transform: rotate(180deg);
|
||||
border-width: 0.75rem;
|
||||
border-style: solid;
|
||||
border-color: var(--Base-Surface-Primary-light-Normal) transparent
|
||||
transparent transparent;
|
||||
}
|
||||
|
||||
.button {
|
||||
grid-template-columns: repeat(3, max-content);
|
||||
font-size: var(--typography-Body-Bold-fontSize);
|
||||
font-family: var(--typography-Body-Bold-fontFamily);
|
||||
}
|
||||
|
||||
.link.active:not(:hover) {
|
||||
background-color: transparent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { cva } from "class-variance-authority"
|
||||
|
||||
import styles from "./languageSwitcher.module.css"
|
||||
|
||||
export const languageSwitcherContentVariants = cva(styles.languageSwitcher, {
|
||||
variants: {
|
||||
variant: {
|
||||
mobileHeader: styles.mobileHeader,
|
||||
mobileFooter: styles.mobileFooter,
|
||||
desktopHeader: styles.desktopHeader,
|
||||
desktopFooter: styles.desktopFooter,
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "desktopHeader",
|
||||
},
|
||||
})
|
||||
@@ -1,44 +1,27 @@
|
||||
"use client"
|
||||
|
||||
import Link from "next/link"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { Lang, languages } from "@/constants/languages"
|
||||
import { languages } from "@/constants/languages"
|
||||
import useDropdownStore from "@/stores/main-menu"
|
||||
|
||||
import {
|
||||
CheckIcon,
|
||||
ChevronDownIcon,
|
||||
ChevronLeftIcon,
|
||||
GlobeIcon,
|
||||
} from "@/components/Icons"
|
||||
import { useHandleKeyUp } from "@/hooks/useHandleKeyUp"
|
||||
import { ChevronDownIcon, GlobeIcon } from "@/components/Icons"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { useTrapFocus } from "@/hooks/useTrapFocus"
|
||||
|
||||
import Subtitle from "../TempDesignSystem/Text/Subtitle"
|
||||
import LanguageSwitcherContent from "./LanguageSwitcherContent"
|
||||
|
||||
import styles from "./languageSwitcher.module.css"
|
||||
|
||||
import { LanguageSwitcherProps } from "@/types/components/current/languageSwitcher"
|
||||
import { LanguageSwitcherProps } from "@/types/components/header/languageSwitcher"
|
||||
|
||||
export default function LanguageSwitcher({
|
||||
urls,
|
||||
location = "header",
|
||||
variant,
|
||||
}: LanguageSwitcherProps) {
|
||||
const intl = useIntl()
|
||||
const languageSwitcherRef = useTrapFocus()
|
||||
const currentLanguage = useLang()
|
||||
const { toggleLanguageSwitcher, isLanguageSwitcherOpen } = useDropdownStore()
|
||||
|
||||
const urlKeys = Object.keys(urls) as Lang[]
|
||||
|
||||
useHandleKeyUp((event: KeyboardEvent) => {
|
||||
if (event.key === "Escape" && isLanguageSwitcherOpen) {
|
||||
toggleLanguageSwitcher()
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<div className={styles.languageSwitcher}>
|
||||
<button
|
||||
@@ -63,46 +46,10 @@ export default function LanguageSwitcher({
|
||||
|
||||
<div
|
||||
className={`${styles.dropdown} ${isLanguageSwitcherOpen ? styles.isExpanded : ""}`}
|
||||
ref={languageSwitcherRef}
|
||||
>
|
||||
{location === "header" ? (
|
||||
<div className={styles.backWrapper}>
|
||||
<button
|
||||
type="button"
|
||||
className={styles.backButton}
|
||||
onClick={toggleLanguageSwitcher}
|
||||
>
|
||||
<ChevronLeftIcon color="red" />
|
||||
<Subtitle type="one">Main Menu</Subtitle>
|
||||
</button>
|
||||
</div>
|
||||
{isLanguageSwitcherOpen ? (
|
||||
<LanguageSwitcherContent urls={urls} variant={variant} />
|
||||
) : null}
|
||||
|
||||
<div className={styles.languageWrapper}>
|
||||
<Subtitle className={styles.subtitle} type="two">
|
||||
{intl.formatMessage({ id: "Select your language" })}
|
||||
</Subtitle>
|
||||
<ul className={styles.list}>
|
||||
{urlKeys.map((key) => {
|
||||
const url = urls[key]?.url
|
||||
const isActive = currentLanguage === key
|
||||
if (url) {
|
||||
return (
|
||||
<li key={key}>
|
||||
<Link
|
||||
className={`${styles.link} ${isActive ? styles.active : ""}`}
|
||||
color="burgundy"
|
||||
href={url}
|
||||
>
|
||||
{languages[key]}
|
||||
{isActive ? <CheckIcon color="burgundy" /> : null}
|
||||
</Link>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
17
components/LanguageSwitcher/variants.ts
Normal file
17
components/LanguageSwitcher/variants.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { cva } from "class-variance-authority"
|
||||
|
||||
import styles from "./languageSwitcher.module.css"
|
||||
|
||||
export const languageSwitcherVariants = cva(styles.languageSwitcher, {
|
||||
variants: {
|
||||
variant: {
|
||||
mobileHeader: styles.mobileHeader,
|
||||
mobileFooter: styles.mobileFooter,
|
||||
desktopHeader: styles.desktopHeader,
|
||||
desktopFooter: styles.desktopFooter,
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "desktopHeader",
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user