From bba56d0742986c494ebc11b522b326846495eefe Mon Sep 17 00:00:00 2001 From: ndrooo Date: Thu, 8 Aug 2024 13:26:30 -0400 Subject: [PATCH] Fix semantics of expandable sections (explorer, toc). This adds the appropriate aria attributes for the [disclosure pattern](https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/examples/disclosure-image-description/#javascriptandcsssourcecode) and uses `visibility: hidden` to remove the hidden elements from the focus order without disrupting the animations. Further work is needed on the tree view nodes. --- quartz/components/Explorer.tsx | 2 ++ quartz/components/TableOfContents.tsx | 8 +++++++- quartz/components/scripts/explorer.inline.ts | 4 ++++ quartz/components/scripts/toc.inline.ts | 4 ++++ quartz/components/styles/explorer.scss | 8 +++++++- quartz/components/styles/toc.scss | 8 +++++++- 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/quartz/components/Explorer.tsx b/quartz/components/Explorer.tsx index 24583a1f7..e4c3dfae5 100644 --- a/quartz/components/Explorer.tsx +++ b/quartz/components/Explorer.tsx @@ -91,6 +91,8 @@ export default ((userOpts?: Partial) => { data-collapsed={opts.folderDefaultState} data-savestate={opts.useSavedState} data-tree={jsonTree} + aria-controls="explorer-content" + aria-expanded={opts.folderDefaultState === "open"} >

{opts.title ?? i18n(cfg.locale).components.explorer.title}

-

{i18n(cfg.locale).components.tableOfContents.title}

{ function toggleExplorer(this: HTMLElement) { this.classList.toggle("collapsed") + this.setAttribute( + "aria-expanded", + this.getAttribute("aria-expanded") === "true" ? "false" : "true", + ) const content = this.nextElementSibling as MaybeHTMLElement if (!content) return diff --git a/quartz/components/scripts/toc.inline.ts b/quartz/components/scripts/toc.inline.ts index 546859ed3..acc81b20d 100644 --- a/quartz/components/scripts/toc.inline.ts +++ b/quartz/components/scripts/toc.inline.ts @@ -16,6 +16,10 @@ const observer = new IntersectionObserver((entries) => { function toggleToc(this: HTMLElement) { this.classList.toggle("collapsed") + this.setAttribute( + "aria-expanded", + this.getAttribute("aria-expanded") === "true" ? "false" : "true", + ) const content = this.nextElementSibling as HTMLElement | undefined if (!content) return content.classList.toggle("collapsed") diff --git a/quartz/components/styles/explorer.scss b/quartz/components/styles/explorer.scss index 6aa109140..314bdc533 100644 --- a/quartz/components/styles/explorer.scss +++ b/quartz/components/styles/explorer.scss @@ -45,8 +45,14 @@ button#explorer { list-style: none; overflow: hidden; max-height: none; - transition: max-height 0.35s ease; + transition: max-height 0.35s ease, visibility 0s linear 0s; margin-top: 0.5rem; + visibility: visible; + + &.collapsed { + transition: max-height 0.35s ease, visibility 0s linear 0.35s; + visibility: hidden; + } &.collapsed > .overflow::after { opacity: 0; diff --git a/quartz/components/styles/toc.scss b/quartz/components/styles/toc.scss index 27ff62a40..c48d9620b 100644 --- a/quartz/components/styles/toc.scss +++ b/quartz/components/styles/toc.scss @@ -29,8 +29,14 @@ button#toc { list-style: none; overflow: hidden; max-height: none; - transition: max-height 0.5s ease; + transition: max-height 0.5s ease, visibility 0s linear 0s; position: relative; + visibility: visible; + + &.collapsed { + transition: max-height 0.5s ease, visibility 0s linear 0.5s; + visibility: hidden; + } &.collapsed > .overflow::after { opacity: 0;