Skip to content

Commit

Permalink
fix: a11y
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Jun 12, 2023
1 parent 97e0bb2 commit 8c9b062
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/app/notes/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useParams } from 'next/navigation'
import { Toc, TocAutoScroll } from '~/components/widgets/toc'
import { useNoteByNidQuery } from '~/hooks/data/use-note'
import { PageDataHolder } from '~/lib/page-holder'
import { ArticleElementContextProvider } from '~/providers/article/article-element-provider'
import { ArticleElementProvider } from '~/providers/article/article-element-provider'
import { NoteLayoutRightSidePortal } from '~/providers/note/right-side-provider'
import { parseMarkdown } from '~/remark'

Expand All @@ -21,14 +21,14 @@ const PageImpl = () => {
<h1>{data?.data?.title}</h1>
</header>

<ArticleElementContextProvider>
<ArticleElementProvider>
{mardownResult.jsx}

<NoteLayoutRightSidePortal>
<Toc className="sticky top-20 mt-20" />
<TocAutoScroll />
</NoteLayoutRightSidePortal>
</ArticleElementContextProvider>
</ArticleElementProvider>
</article>
)
}
Expand Down
31 changes: 15 additions & 16 deletions src/components/ui/float-popover/FloatPopover.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use client'

import { flip, offset, shift, useFloating } from '@floating-ui/react-dom'
import React, {
Fragment,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import clsx from 'clsx'
import type { UseFloatingOptions } from '@floating-ui/react-dom'
import type { FC, PropsWithChildren } from 'react'
Expand All @@ -21,7 +14,7 @@ import styles from './index.module.css'

export const FloatPopover: FC<
PropsWithChildren<{
triggerComponent: FC
TriggerComponent: FC
headless?: boolean
wrapperClassNames?: string
trigger?: 'click' | 'hover' | 'both'
Expand All @@ -43,7 +36,7 @@ export const FloatPopover: FC<
const {
headless = false,
wrapperClassNames,
triggerComponent: TriggerComponent,
TriggerComponent,
trigger = 'hover',
padding,
offset: offsetValue,
Expand Down Expand Up @@ -96,10 +89,10 @@ export const FloatPopover: FC<

if (status === 'in') {
nextElementSibling.ontransitionend = null
nextElementSibling?.classList.add(styles.show)
nextElementSibling.classList.add(styles.show)
} else {
nextElementSibling?.classList.remove(styles.show)
nextElementSibling!.ontransitionend = () => {
nextElementSibling.classList.remove(styles.show)
nextElementSibling.ontransitionend = () => {
setOpen(false)
setMounted(false)
}
Expand Down Expand Up @@ -201,6 +194,12 @@ export const FloatPopover: FC<
</As>
)

useEffect(() => {
if (containerRef.current && open) {
containerRef.current.focus()
}
}, [open])

if (!props.children) {
return TriggerWrapper
}
Expand All @@ -219,15 +218,15 @@ export const FloatPopover: FC<
)}
{...(trigger === 'hover' || trigger === 'both' ? listener : {})}
ref={containerRef}
tabIndex={0}
role="tooltip"
tabIndex={-1}
role="dialog"
aria-modal="true"
>
<div ref={setContainerAnchorRef} />
{open && (
<div
className={clsxm(
'bg-neutral',
'bg-base-100',
headless ? styles['headless'] : styles['popover-root'],
animate && styles['animate'],
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/note/NoteTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const MemoedItem = memo<{
as="span"
className="inline-flex items-center"
>
<i className="icon-[material-symbols--arrow-circle-right-outline-rounded]" />
<i className="icon-[material-symbols--arrow-circle-right-outline-rounded] duration-200" />
</LeftToRightTransitionView>
<Link
className={clsxm(
Expand Down
8 changes: 4 additions & 4 deletions src/components/widgets/note/NoteTopicInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export const NoteTopicInfo = () => {
<p className="mb-1 flex min-w-0 flex-col overflow-hidden text-neutral-content/50">
此文章收录于专栏:
</p>

<FloatPopover
placement="right"
strategy="fixed"
wrapperClassNames="flex flex-grow flex-shrink min-w-0"
triggerComponent={ToTopicLink}
TriggerComponent={ToTopicLink}
>
<InnerTopicDetail topic={note.topic} />
</FloatPopover>
Expand All @@ -53,8 +54,8 @@ const InnerTopicDetail: FC<{ topic: TopicModel }> = (props) => {

return (
<div className="flex w-[400px] flex-col">
<Link href={`/notes/topics/${topic.slug}`} tabIndex={99}>
<h1 className="!m-0 inline-block py-2 text-lg font-medium">
<Link href={`/notes/topics/${topic.slug}`}>
<h1 className="!m-0 inline-block pb-2 text-lg font-medium">
{topic.name}
</h1>
</Link>
Expand Down Expand Up @@ -85,7 +86,6 @@ const InnerTopicDetail: FC<{ topic: TopicModel }> = (props) => {
<DividerVertical />
<span className="inline-flex min-w-0 flex-shrink">
<Link
tabIndex={99}
href={`/data?.data/${data?.data[0].nid}`}
className="truncate"
>
Expand Down
57 changes: 45 additions & 12 deletions src/providers/article/article-element-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,70 @@
import { useEffect, useState } from 'react'
import { useEffect, useRef } from 'react'
import { createContextState } from 'foxact/create-context-state'

import { clsxm } from '~/utils/helper'

const [
ArticleElementContextProviderInternal,
ArticleElementProviderInternal,
useArticleElement,
useSetArticleElement,
] = createContextState<HTMLDivElement | null>(undefined as any)

const ArticleElementContextProvider: Component = ({ children, className }) => {
const [
IsEOArticleElementProviderInternal,
useIsEOArticleElement,
useSetIsEOArticleElement,
] = createContextState<boolean>(false)

const ArticleElementProvider: Component = ({ children, className }) => {
return (
<ArticleElementContextProviderInternal>
<Content className={className}>{children}</Content>
</ArticleElementContextProviderInternal>
<ArticleElementProviderInternal>
<IsEOArticleElementProviderInternal>
<Content className={className}>{children}</Content>
</IsEOArticleElementProviderInternal>
</ArticleElementProviderInternal>
)
}

const Content: Component = ({ children, className }) => {
const [contentRef, setContentRef] = useState<HTMLDivElement | null>(null)
const setter = useSetArticleElement()
useEffect(() => {
setter(contentRef)
}, [contentRef, setter])

return (
<div className={clsxm('relative', className)} ref={setContentRef}>
<div className={clsxm('relative', className)} ref={setter}>
{children}
<EOADetector />
</div>
)
}

const EOADetector: Component = () => {
const ref = useRef<HTMLDivElement>(null)
const setter = useSetIsEOArticleElement()
useEffect(() => {
if (!ref.current) return
const $el = ref.current
const observer = new IntersectionObserver(
(entries) => {
const entry = entries[0]
setter(entry.isIntersecting)
},
{
rootMargin: '0px 0px 10% 0px',
},
)

observer.observe($el)
return () => {
observer.unobserve($el)
observer.disconnect()
}
}, [])

return <div ref={ref} />
}

export {
ArticleElementContextProvider,
ArticleElementProvider,
useSetArticleElement,
useArticleElement,
useIsEOArticleElement,
}
4 changes: 0 additions & 4 deletions src/styles/tailwindcss.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 8c9b062

@vercel
Copy link

@vercel vercel bot commented on 8c9b062 Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

springtide – ./

springtide.vercel.app
springtide-git-main-innei.vercel.app
springtide-innei.vercel.app

Please sign in to comment.