|
| 1 | +{/* Copyright 2023 Adobe. All rights reserved. |
| 2 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 4 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 6 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 7 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 8 | +governing permissions and limitations under the License. */} |
| 9 | + |
| 10 | +import {Layout} from '@react-spectrum/docs'; |
| 11 | +export default Layout; |
| 12 | + |
| 13 | +import docs from 'docs:@react-aria/landmark'; |
| 14 | +import {HeaderInfo, FunctionAPI, PageDescription} from '@react-spectrum/docs'; |
| 15 | +import {Keyboard} from '@react-spectrum/text'; |
| 16 | +import packageData from '@react-aria/landmark/package.json'; |
| 17 | + |
| 18 | +--- |
| 19 | +category: Utilities |
| 20 | +keywords: [landmarks, aria] |
| 21 | +--- |
| 22 | + |
| 23 | +# useLandmark |
| 24 | + |
| 25 | +<PageDescription>{docs.exports.useLandmark.description}</PageDescription> |
| 26 | + |
| 27 | +<HeaderInfo |
| 28 | + packageData={packageData} |
| 29 | + componentNames={['useLandmark']} |
| 30 | + sourceData={[ |
| 31 | + {type: 'W3C', url: 'https://www.w3.org/WAI/ARIA/apg/practices/landmark-regions/'} |
| 32 | + ]} /> |
| 33 | + |
| 34 | +## API |
| 35 | + |
| 36 | +<FunctionAPI function={docs.exports.useLandmark} links={docs.links} /> |
| 37 | + |
| 38 | +## Features |
| 39 | + |
| 40 | +Landmarks provide a way to designate important subsections of a page. They allow screen reader users to get an overview of the various sections of the page, and jump to a specific section. |
| 41 | +By default, browsers do not provide a consistent way to navigate between landmarks using the keyboard. |
| 42 | +The `useLandmark` hook enables keyboard navigation between landmarks, and provides a consistent experience across browsers. |
| 43 | + |
| 44 | +* <Keyboard>F6</Keyboard> and <Keyboard>Shift+F6</Keyboard> key navigation between landmarks |
| 45 | +* <Keyboard>Alt+F6</Keyboard> key navigation to the main landmark |
| 46 | +* Support for navigating nested landmarks |
| 47 | + |
| 48 | +## Anatomy |
| 49 | + |
| 50 | +Landmark elements can be registered with the `useLandmark` hook. The `role` prop is required. |
| 51 | + |
| 52 | +Pressing <Keyboard>F6</Keyboard> will move focus to the next landmark on the page, and pressing <Keyboard>Shift+F6</Keyboard> will move focus to the previous landmark. |
| 53 | +If an element within a landmark was previously focused before leaving that landmark, focus will return to that element when navigating back to that landmark. |
| 54 | +<Keyboard>Alt+F6</Keyboard> will always move focus to the main landmark if it has been registered. |
| 55 | + |
| 56 | +If multiple landmarks are registered with the same role, they should have unique labels, which can be provided by aria-label or aria-labelledby. |
| 57 | + |
| 58 | +For an example of landmarks in use, see the [useToastRegion](useToast.html#anatomy) documentation. |
| 59 | + |
| 60 | +## Example |
| 61 | + |
| 62 | +```tsx example export=true |
| 63 | +import {useLandmark} from '@react-aria/landmark'; |
| 64 | +import {useRef} from 'react'; |
| 65 | + |
| 66 | +function Navigation(props) { |
| 67 | + let ref = useRef(); |
| 68 | + let {landmarkProps} = useLandmark({...props, role: 'navigation'}, ref); |
| 69 | + return ( |
| 70 | + <nav ref={ref} {...props} {...landmarkProps}> |
| 71 | + {props.children} |
| 72 | + </nav> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +function Region(props) { |
| 77 | + let ref = useRef(); |
| 78 | + let {landmarkProps} = useLandmark({...props, role: 'region'}, ref); |
| 79 | + return ( |
| 80 | + <article ref={ref} {...props} {...landmarkProps}> |
| 81 | + {props.children} |
| 82 | + </article> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +function Search(props) { |
| 87 | + let ref = useRef(); |
| 88 | + let {landmarkProps} = useLandmark({...props, role: 'search'}, ref); |
| 89 | + return ( |
| 90 | + <form ref={ref} {...props} {...landmarkProps}> |
| 91 | + <h2 id="search-header">Search</h2> |
| 92 | + <input aria-labelledby="search-header" type="search" /> |
| 93 | + </form> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +<div> |
| 98 | + <Navigation> |
| 99 | + <h2>Navigation</h2> |
| 100 | + <ul> |
| 101 | + <li><a href="#">Link 1</a></li> |
| 102 | + <li><a href="#">Link 2</a></li> |
| 103 | + </ul> |
| 104 | + </Navigation> |
| 105 | + <Search /> |
| 106 | + <Region> |
| 107 | + <h2>Region</h2> |
| 108 | + <p>Example region with no focusable children.</p> |
| 109 | + </Region> |
| 110 | +</div> |
| 111 | +``` |
0 commit comments