-
Notifications
You must be signed in to change notification settings - Fork 212
[Chakra V3 Upgrade 🎨] Create Skip Nav Components (@W-18507873@) #2757
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
Changes from 7 commits
20b7a66
e8a4361
7133064
a7c73b8
4f5921d
a966353
8a5bcea
5cf7965
b007137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| import React from 'react' | ||
| import {screen} from '@testing-library/react' | ||
| import {SkipNavLink, SkipNavContent} from './index' | ||
| import {renderWithProviders} from '../../utils/test-utils' | ||
|
|
||
| describe('SkipNavLink', () => { | ||
| test('renders with default props', () => { | ||
| renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Content'}) | ||
| expect(link).toBeInTheDocument() | ||
| expect(link).toHaveAttribute('href', '#skip-to-content') | ||
| }) | ||
|
|
||
| test('renders with custom href', () => { | ||
| renderWithProviders(<SkipNavLink href="#main-content">Skip to Main</SkipNavLink>, {}) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Main'}) | ||
| expect(link).toHaveAttribute('href', '#main-content') | ||
| }) | ||
|
|
||
| test('has correct accessibility attributes', () => { | ||
| renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Content'}) | ||
| expect(link).toHaveAttribute('href', '#skip-to-content') | ||
| }) | ||
|
|
||
| test('has visually hidden styles by default', () => { | ||
| renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Content'}) | ||
|
|
||
| // The link should be focusable (no aria-hidden) but positioned off-screen | ||
| expect(link).not.toHaveAttribute('aria-hidden') | ||
| expect(link).toHaveAttribute('href', '#skip-to-content') | ||
| }) | ||
|
|
||
| test('uses theme styles', () => { | ||
| renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Content'}) | ||
| expect(link).toBeInTheDocument() | ||
| // The component should use theme styles from the skipNav slot recipe | ||
| expect(link).toHaveAttribute('href', '#skip-to-content') | ||
| }) | ||
| }) | ||
|
|
||
| describe('SkipNavContent', () => { | ||
| test('renders with default props', () => { | ||
| renderWithProviders( | ||
| <SkipNavContent> | ||
| <div>Main content</div> | ||
| </SkipNavContent>, | ||
| {} | ||
| ) | ||
|
|
||
| const content = screen.getByText('Main content') | ||
| const container = content.parentElement | ||
|
|
||
| expect(container).toHaveAttribute('id', 'skip-to-content') | ||
| expect(container).toHaveAttribute('tabIndex', '-1') | ||
| }) | ||
|
|
||
| test('renders with custom id', () => { | ||
| renderWithProviders( | ||
| <SkipNavContent id="main-content"> | ||
| <div>Main content</div> | ||
| </SkipNavContent>, | ||
| {} | ||
| ) | ||
|
|
||
| const content = screen.getByText('Main content') | ||
| const container = content.parentElement | ||
|
|
||
| expect(container).toHaveAttribute('id', 'main-content') | ||
| expect(container).toHaveAttribute('tabIndex', '-1') | ||
| }) | ||
|
|
||
| test('has correct accessibility attributes', () => { | ||
| renderWithProviders( | ||
| <SkipNavContent> | ||
| <div>Main content</div> | ||
| </SkipNavContent>, | ||
| {} | ||
| ) | ||
|
|
||
| const content = screen.getByText('Main content') | ||
| const container = content.parentElement | ||
|
|
||
| expect(container).toHaveAttribute('id', 'skip-to-content') | ||
| expect(container).toHaveAttribute('tabIndex', '-1') | ||
| }) | ||
|
|
||
| test('uses theme styles', () => { | ||
| renderWithProviders( | ||
| <SkipNavContent> | ||
| <div>Content with theme</div> | ||
| </SkipNavContent>, | ||
| {} | ||
| ) | ||
|
|
||
| const content = screen.getByText('Content with theme') | ||
| const container = content.parentElement | ||
|
|
||
| expect(container).toBeInTheDocument() | ||
| // The component should use theme styles from the skipNav slot recipe | ||
| expect(container).toHaveAttribute('id', 'skip-to-content') | ||
| }) | ||
| }) | ||
|
|
||
| describe('SkipNavLink and SkipNavContent integration', () => { | ||
| test('link href matches content id', () => { | ||
| renderWithProviders( | ||
| <div> | ||
| <SkipNavLink>Skip to Content</SkipNavLink> | ||
| <SkipNavContent> | ||
| <div>Main content</div> | ||
| </SkipNavContent> | ||
| </div>, | ||
| {} | ||
| ) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Content'}) | ||
| const content = screen.getByText('Main content') | ||
| const container = content.parentElement | ||
|
|
||
| expect(link).toHaveAttribute('href', '#skip-to-content') | ||
| expect(container).toHaveAttribute('id', 'skip-to-content') | ||
| }) | ||
|
|
||
| test('custom href and id work together', () => { | ||
| renderWithProviders( | ||
| <div> | ||
| <SkipNavLink href="#main">Skip to Main</SkipNavLink> | ||
| <SkipNavContent id="main"> | ||
| <div>Main content</div> | ||
| </SkipNavContent> | ||
| </div>, | ||
| {} | ||
| ) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Main'}) | ||
| const content = screen.getByText('Main content') | ||
| const container = content.parentElement | ||
|
|
||
| expect(link).toHaveAttribute('href', '#main') | ||
| expect(container).toHaveAttribute('id', 'main') | ||
| }) | ||
|
|
||
| test('both components use skip-nav theme styles', () => { | ||
| renderWithProviders( | ||
| <div> | ||
| <SkipNavLink>Skip to Content</SkipNavLink> | ||
| <SkipNavContent> | ||
| <div>Main content</div> | ||
| </SkipNavContent> | ||
| </div>, | ||
| {} | ||
| ) | ||
|
|
||
| const link = screen.getByRole('link', {name: 'Skip to Content'}) | ||
| const content = screen.getByText('Main content') | ||
| const container = content.parentElement | ||
|
|
||
| // Both components should be rendered and use theme styles | ||
| expect(link).toBeInTheDocument() | ||
| expect(container).toBeInTheDocument() | ||
| expect(link).toHaveAttribute('href', '#skip-to-content') | ||
| expect(container).toHaveAttribute('id', 'skip-to-content') | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| import React from 'react' | ||
| import {Box, Link, useSlotRecipe} from '@chakra-ui/react' | ||
|
|
||
| interface SkipNavLinkProps { | ||
| children: any | ||
| href?: string | ||
| [key: string]: any | ||
| } | ||
|
|
||
| interface SkipNavContentProps { | ||
| children: any | ||
| css?: any | ||
| id?: string | ||
| [key: string]: any | ||
| } | ||
|
|
||
| /** | ||
| * SkipNavLink component provides a skip link for keyboard navigation | ||
| * with initial state screen reader accessible but visually hidden | ||
| */ | ||
| export const SkipNavLink = ({children, href = '#skip-to-content', ...props}: SkipNavLinkProps) => { | ||
| const recipe = useSlotRecipe({key: 'skipNav'}) | ||
| const styles = recipe() | ||
|
|
||
| return ( | ||
| <Link href={href} css={styles.link} {...props}> | ||
| {children} | ||
| </Link> | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * SkipNavContent component provides the target content area for skip navigation | ||
| */ | ||
| export const SkipNavContent = ({ | ||
| children, | ||
| css, | ||
| id = 'skip-to-content', | ||
| ...props | ||
| }: SkipNavContentProps) => { | ||
| const recipe = useSlotRecipe({key: 'skipNav'}) | ||
| const styles = recipe() | ||
|
|
||
| return ( | ||
| <Box id={id} css={{...styles.content, ...css}} tabIndex={-1} {...props}> | ||
| {children} | ||
| </Box> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import {defineSlotRecipe} from '@chakra-ui/react' | ||
|
|
||
| export default defineSlotRecipe({ | ||
| slots: ['link', 'content'], | ||
| base: { | ||
| link: { | ||
| position: 'absolute', | ||
| zIndex: 'skipNav', | ||
| left: '-10000px', | ||
| top: 'auto', | ||
| width: '1px', | ||
| height: '1px', | ||
| overflow: 'hidden', | ||
| _focusVisible: { | ||
| // !important needed to override the default hidden positioning (absolute + left: -10000px) | ||
| position: 'fixed !important', | ||
| top: '6px !important', | ||
| left: '6px !important', | ||
| width: 'auto !important', | ||
| height: 'auto !important', | ||
| overflow: 'visible !important', | ||
| zIndex: 'skipNav', | ||
| padding: '8px', | ||
|
||
| backgroundColor: 'white', | ||
| color: 'black', | ||
| textDecoration: 'none', | ||
| border: '2px solid black', | ||
| borderRadius: '4px', | ||
| fontSize: '14px', | ||
|
||
| fontWeight: 'bold', | ||
| outline: 'none', | ||
| boxShadow: '0 2px 4px rgba(0, 0, 0, 0.1)', | ||
| whiteSpace: 'nowrap' | ||
| }, | ||
| _focus: { | ||
| // !important needed to override the default hidden positioning (absolute + left: -10000px) | ||
| position: 'fixed !important', | ||
| top: '6px !important', | ||
| left: '6px !important', | ||
| width: 'auto !important', | ||
| height: 'auto !important', | ||
| overflow: 'visible !important', | ||
| zIndex: 'skipNav', | ||
| padding: '8px', | ||
| backgroundColor: 'white', | ||
| color: 'black', | ||
| textDecoration: 'none', | ||
| border: '2px solid black', | ||
| borderRadius: '4px', | ||
| fontSize: '14px', | ||
|
||
| fontWeight: 'bold', | ||
| outline: 'none', | ||
| boxShadow: '0 2px 4px rgba(0, 0, 0, 0.1)', | ||
| whiteSpace: 'nowrap' | ||
| } | ||
| }, | ||
| content: {} | ||
| } | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.