-
Notifications
You must be signed in to change notification settings - Fork 4
fix(ui): update table rendering behavior #366
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
sneumannb5
wants to merge
6
commits into
byte5ai:main
Choose a base branch
from
sneumannb5:fix/ui-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−7
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2314b27
fix(ui): update table rendering behavior
sneumannb5 4b34ab0
fix(ui): update ui changes to requested scope
sneumannb5 e3ba05a
fix: implement feedback
sneumannb5 1d03953
Merge branch 'main' into fix/ui-updates
ConnysCode 8eb2865
fix(ui): make scrollability of table optional
sneumannb5 2c6fd11
fix(ui): update tests
sneumannb5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 'use client'; | ||
|
|
||
| import type { ReactNode } from 'react'; | ||
| import { useLayoutEffect, useRef, useState } from 'react'; | ||
| import { useTranslations } from 'next-intl'; | ||
|
|
||
| interface TableProps { | ||
| children?: ReactNode; | ||
| className?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Custom `<table>` renderer for {@link Markdown}. Wraps the GFM table in a | ||
| * scroll-container so very wide / very long tables stay inside their | ||
| * surrounding block: horizontal scroll for many columns or long | ||
| * compound-word cells, vertical scroll with a sticky `<thead>` for many | ||
| * rows. The wrapper is keyboard-focusable so arrow-key scrolling works | ||
| * for keyboard-only users. | ||
| * | ||
| * The scroll-region affordance (focusable + labelled group) is only applied | ||
| * when the wrapper actually overflows, so a table that fits its container | ||
| * doesn't consume a tab stop or announce a redundant group (WCAG 2.1.1). | ||
| */ | ||
| export function MarkdownTable({ | ||
| children, | ||
| className, | ||
| }: TableProps): React.ReactElement { | ||
| const t = useTranslations('markdownTable'); | ||
| const ref = useRef<HTMLDivElement>(null); | ||
| const [overflows, setOverflows] = useState(false); | ||
| useLayoutEffect(() => { | ||
| const el = ref.current; | ||
| if (!el) return; | ||
| const measure = (): void => | ||
| setOverflows( | ||
| el.scrollWidth > el.clientWidth || el.scrollHeight > el.clientHeight, | ||
| ); | ||
| measure(); | ||
| const ro = new ResizeObserver(measure); | ||
| ro.observe(el); | ||
| return () => ro.disconnect(); | ||
| }, []); | ||
| return ( | ||
| <div | ||
| ref={ref} | ||
| className="md-table-wrap" | ||
| tabIndex={overflows ? 0 : undefined} | ||
| role={overflows ? 'group' : undefined} | ||
| aria-label={overflows ? t('scrollRegionLabel') : undefined} | ||
| > | ||
| <table className={className}>{children}</table> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { renderWithIntl } from '../../_lib/test-utils'; | ||
| import { MarkdownTable } from '../MarkdownTable'; | ||
|
|
||
| /** | ||
| * MarkdownTable wraps the GFM `<table>` in a focusable scroll container. | ||
| * Asserts the wrapper contract: container class, accessibility attrs from | ||
| * the i18n key, and that the inner table receives the className passed by | ||
| * the react-markdown override slot. | ||
| */ | ||
| describe('<MarkdownTable />', () => { | ||
| it('wraps children in a .md-table-wrap div with the inner table', () => { | ||
| const { container } = renderWithIntl( | ||
| <MarkdownTable> | ||
| <tbody> | ||
| <tr> | ||
| <td>cell</td> | ||
| </tr> | ||
| </tbody> | ||
| </MarkdownTable>, | ||
| ); | ||
| const wrap = container.querySelector('div.md-table-wrap'); | ||
| expect(wrap).not.toBeNull(); | ||
| expect(wrap?.querySelector('table')).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('omits the scroll-region affordance when the table fits its container', () => { | ||
| // jsdom reports 0 for all scroll/client dims, so the wrapper never | ||
| // overflows: no tab stop, no group role, no aria-label (WCAG 2.1.1). | ||
| const { container } = renderWithIntl( | ||
| <MarkdownTable> | ||
| <tbody> | ||
| <tr> | ||
| <td>cell</td> | ||
| </tr> | ||
| </tbody> | ||
| </MarkdownTable>, | ||
| ); | ||
| const wrap = container.querySelector('div.md-table-wrap'); | ||
| expect(wrap?.getAttribute('role')).toBeNull(); | ||
| expect(wrap?.getAttribute('tabindex')).toBeNull(); | ||
| expect(wrap?.getAttribute('aria-label')).toBeNull(); | ||
| }); | ||
|
|
||
| it('exposes a focusable group with an i18n aria-label when the table overflows', () => { | ||
| // Force horizontal overflow: scrollWidth > clientWidth at mount. | ||
| const proto = window.HTMLElement.prototype; | ||
| const sw = Object.getOwnPropertyDescriptor(proto, 'scrollWidth'); | ||
| const cw = Object.getOwnPropertyDescriptor(proto, 'clientWidth'); | ||
| Object.defineProperty(proto, 'scrollWidth', { configurable: true, value: 200 }); | ||
| Object.defineProperty(proto, 'clientWidth', { configurable: true, value: 100 }); | ||
| try { | ||
| const { container } = renderWithIntl( | ||
| <MarkdownTable> | ||
| <tbody> | ||
| <tr> | ||
| <td>cell</td> | ||
| </tr> | ||
| </tbody> | ||
| </MarkdownTable>, | ||
| ); | ||
| const wrap = container.querySelector('div.md-table-wrap'); | ||
| expect(wrap?.getAttribute('role')).toBe('group'); | ||
| expect(wrap?.getAttribute('tabindex')).toBe('0'); | ||
| expect(wrap?.getAttribute('aria-label')).toBe('Scrollable table'); | ||
| } finally { | ||
| if (sw) Object.defineProperty(proto, 'scrollWidth', sw); | ||
| else delete (proto as { scrollWidth?: unknown }).scrollWidth; | ||
| if (cw) Object.defineProperty(proto, 'clientWidth', cw); | ||
| else delete (proto as { clientWidth?: unknown }).clientWidth; | ||
| } | ||
| }); | ||
|
|
||
| it('forwards className to the inner <table>', () => { | ||
| const { container } = renderWithIntl( | ||
| <MarkdownTable className="custom-class"> | ||
| <tbody> | ||
| <tr> | ||
| <td>cell</td> | ||
| </tr> | ||
| </tbody> | ||
| </MarkdownTable>, | ||
| ); | ||
| const table = container.querySelector('table'); | ||
| expect(table?.className).toBe('custom-class'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,11 @@ | ||
| import '@testing-library/jest-dom/vitest'; | ||
|
|
||
| // jsdom has no ResizeObserver; stub it so components that observe element | ||
| // size (e.g. MarkdownTable's overflow detection) can mount in tests. | ||
| if (!('ResizeObserver' in globalThis)) { | ||
| globalThis.ResizeObserver = class { | ||
| observe(): void {} | ||
| unobserve(): void {} | ||
| disconnect(): void {} | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.