Anchor Navigation 컴포넌트
Anchor Navigation는 사용자가 미리 정의된 앵커 링크를 통해 페이지의 특정 콘텐츠로 빠르게 이동할 수 있게 해 주는 컴포넌트예요. 페이지의 제목 구조(h2, h3, h4)를 목록으로 보여 주면서, 스크롤 위치에 따라 현재 보고 있는 섹션을 강조 표시해 줘요. 개발 가이드와 단위·통합 테스트 예제, 그리고 디자인·작성·접근성 가이드라인을 함께 제공해요.
출처: 문서
본문
Anchor navigation
Allows users to quickly jump to specific page content via predefined anchor links.
사용자는 미리 정의된 앵커 링크를 통해 페이지의 특정 콘텐츠로 빠르게 이동할 수 있어요.
Development guidelines
Scroll-spy behaviour
기본적으로 컴포넌트에 내장된 scroll-spy는 뷰포트의 상단에 가장 가까운 섹션을 기준으로 활성 앵커를 설정하며, scrollSpyOffset으로 이를 조정할 수 있어요.
내장된 scroll-spy 동작이 특정 요구사항에 맞지 않는다면 activeHref prop을 사용해서 활성 앵커를 수동으로 설정할 수 있어요.
테스트 유틸리티 클래스가 처음이라면 소개 문서에서 더 자세히 알아볼 수 있어요.
Unit testing examples
앵커 내비게이션 선택하기
import { render } from '@testing-library/react';
import createWrapper from '@cloudscape-design/components/test-utils/dom';
import AnchorNavigation from '@cloudscape-design/components/anchor-navigation';
describe('<AnchorNavigation />', () => {
it('renders the anchor-navigation component', () => {
const { container } = render(<AnchorNavigation />);
const wrapper = createWrapper(container);
expect(wrapper.findAnchorNavigation()).toBeTruthy();
});
it('selects all anchor-navigation components', () => {
const { container } = render(<>
<AnchorNavigation />
<AnchorNavigation />
<AnchorNavigation />
</>);
const wrapper = createWrapper(container);
const components = wrapper.findAllAnchorNavigations();
expect(components).toHaveLength(3)
});
});
href로 앵커 아이템 링크 선택하기
import AnchorNavigation from '@cloudscape-design/components/anchor-navigation';
import createWrapper from '@cloudscape-design/components/test-utils/dom';
import { render } from '@testing-library/react';
import React from 'react';
const Component = () => {
return (
<AnchorNavigation
anchors={[
{ text: 'Section 1', href: '#section1', level: 1 },
{ text: 'Section 2', href: '#section2', level: 1 },
{ text: 'Section 3', href: '#section3', level: 1, info: 'Updated' },
{ text: 'Section 4', href: '#section4', level: 1, info: 'New' },
]}
/>
);
};
describe('<AnchorNavigation />', () => {
it('find anchor item link by href', async () => {
const { container } = render(<Component />);
const wrapper = createWrapper(container).findAnchorNavigation();
const updatedSection = wrapper!.findAnchorLinkByHref('#section3')!.getElement();
expect(updatedSection.textContent).toContain('Updated');
});
});
인덱스로 앵커 아이템 선택하기
import AnchorNavigation from '@cloudscape-design/components/anchor-navigation';
import createWrapper from '@cloudscape-design/components/test-utils/dom';
import { render } from '@testing-library/react';
import React from 'react';
const Component = () => {
return (
<AnchorNavigation
anchors={[
{ text: 'Section 1', href: '#section1', level: 1 },
{ text: 'Section 2', href: '#section2', level: 1 },
{ text: 'Section 3', href: '#section3', level: 1, info: 'Updated' },
{ text: 'Section 4', href: '#section4', level: 1, info: 'New' },
]}
/>
);
};
describe('<AnchorNavigation />', () => {
it('find anchor item by index', async () => {
const { container } = render(<Component />);
const wrapper = createWrapper(container).findAnchorNavigation();
const updatedSection = wrapper!.findAnchorByIndex(3)!.findInfo()!.getElement();
const newSection = wrapper!.findAnchorByIndex(4)!.findInfo()?.getElement();
expect(updatedSection?.textContent).toContain('Updated');
expect(newSection?.textContent).toContain('New');
});
});
Unit testing APIs
AnchorNavigationWrapper
| Name | Return type | Description | Parameters |
|---|---|---|---|
| findActiveAnchor | AnchorItemWrapper | null | - | - |
| findAnchorByIndex | AnchorItemWrapper | null | - | index: |
| findAnchorLinkByHref | ElementWrapper | null | - | href: |
| findAnchorNavigation | ElementWrapper | null | - | - |
| findAnchorNavigationList | ElementWrapper | null | - | - |
| findAnchors | Array<AnchorItemWrapper> | - | - |
| Name | Return type | Description | Parameters |
|---|---|---|---|
| findInfo | ElementWrapper | null | - | - |
| findLink | ElementWrapper | null | - | - |
| findText | ElementWrapper | null | - | - |
| isActive | boolean | - | - |
Integration testing examples
앵커 내비게이션 링크와 상호작용하기
import createWrapper from '@cloudscape-design/components/test-utils/selectors';
/**
* browser - is a global object representing your testing framework.
* The exact API could be different for your stack.
*/
describe('Anchor Navigation', () => {
/**
* This code tests a component similar to /components/anchor-navigation
*/
it('clicks on an anchor navigation link', async () => {
// go to a page which contains the autosuggest to test
await browser.url('your-page-to-test');
// always make sure to disambiguate your component to test with a data-testid attribute
const wrapper = createWrapper().findAnchorNavigation('[data-testid="my-autosuggest"]')!;
const linkItem = await browser.$(wrapper.findAnchorByIndex(1).toSelector());
await linkItem.click();
expect(browser.getUrl()).toContain('#NAME_OF_HASH');
});
});
Integration testing APIs
AnchorNavigationWrapper
| Name | Return type | Description | Parameters |
|---|---|---|---|
| findActiveAnchor | AnchorItemWrapper | - | - |
| findAnchorByIndex | AnchorItemWrapper | - | index: |
| findAnchorLinkByHref | ElementWrapper | - | href: |
| findAnchorNavigation | ElementWrapper | - | - |
| findAnchorNavigationList | ElementWrapper | - | - |
| findAnchors | MultiElementWrapper<AnchorItemWrapper> | - | - |
| Name | Return type | Description | Parameters |
|---|---|---|---|
| findInfo | ElementWrapper | - | - |
| findLink | ElementWrapper | - | - |
| findText | ElementWrapper | - | - |
General guidelines
Do
- 중첩은 최대 3단계로 제한하세요. 예를 들어 h2, h3, h4와 같이요.
- 앵커 링크 텍스트는 링크가 가리키는 제목 텍스트와 일치하도록 하세요. 예를 들어 General guidelines 는 앵커 링크와 콘텐츠 섹션 양쪽의 제목 텍스트예요.
- 링크 목록의 이해도를 높이려면 앵커 내비게이션 컴포넌트를 제목과 함께 사용하세요.
- 앵커 내비게이션을 페이지에서 눈에 보이는 요소로 제공하거나 접근 지점을 제공하세요. 예를 들어 모바일 화면에서는 확장 가능한 섹션에 포함하세요.
Don't
- 애플리케이션의 정보 구조(information architecture)를 표현할 때는 앵커 내비게이션을 사용하지 마세요. 대신 사이드 내비게이션을 사용하세요.
- 페이지 콘텐츠용 필터 목록을 표현할 때는 앵커 내비게이션을 사용하지 마세요.
Features
-
Anchor links
페이지 제목(h2, h3, h4)을 나타내는 앵커 링크 목록이에요. 앵커 링크를 사용하면 해당 콘텐츠 섹션으로 빠르게 이동할 수 있어요. 제목과 하위 제목의 정의는 완전히 자유롭게 제어할 수 있어요.
콘텐츠 영역에 스크롤 모션을 추가할 수도 있지만, 인지 장애가 있는 사용자를 배려하고 운영체제 설정을 존중해야 해요.
-
Active state indicator
선택된 링크의 활성 상태를 강조해 주는 시각적 표시예요.
-
Content status label - optional
새로 만들어졌거나 최근에 수정된 콘텐츠를 강조하려면 해당 앵커 링크 옆에 New 또는 Updated 라벨을 포함하세요. 라벨 유지 기간은 애플리케이션의 릴리스·업데이트 주기에 맞춰 정의하세요. 라벨 노출 최대 기간은 30일을 권장해요.
Additional configurations
-
Headings
앵커 내비게이션과 제목의 조합은 데스크톱과 모바일 기기에서 비슷하게 보이도록 설계되어, 여러 플랫폼에서 일관된 사용자 경험을 제공해요.
- Desktop: 앵커 내비게이션 컴포넌트를 On this page 와 같은 제목과 함께 사용하면, 뒤따르는 링크 목록에 대한 맥락을 제공할 수 있어요.
- Mobile: 앵커 내비게이션을 확장 가능한 섹션 안에 포함할 수 있어요. 콘텐츠 영역의 첫 번째 요소로 배치하고, 기본적으로 섹션은 접힌 상태로 만드세요.
-
Motion
콘텐츠 영역에 스크롤 모션을 추가할 수 있지만, 인지 장애가 있는 사용자를 배려하고 운영체제 설정을 존중해야 해요.
Writing guidelines
General writing guidelines
-
문장형 대문자(sentence case)를 사용하되, 고유명사와 브랜드 이름은 문맥에 맞게 대문자를 계속 사용하세요.
-
현재형 동사와 능동태를 사용하세요.
-
글이나 설명에서 please , thank you , 줄임표( ... ), 앰퍼샌드( & ), e.g. , i.e. , etc. 는 사용하지 마세요.
-
방향을 지칭하는 표현은 피하세요.
- 예를 들어 above 대신 previous 를, below 대신 following 을 사용하세요.
-
기기에 의존하지 않는 언어를 사용하세요.
- 예를 들어 click 대신 choose 또는 select 를 사용하세요.
Component-specific guidelines
Heading
- 앵커 내비게이션을 제목과 함께 사용할 때는 On this page 라는 텍스트를 사용하세요.
Accessibility guidelines
General accessibility guidelines
- 각 컴포넌트의 대체 텍스트와 ARIA(Accessible Rich Internet Applications) 영역에 대한 가이드라인을 따르세요.
- 애플리케이션의 언어 맥락에 맞게 ARIA 라벨을 정의하세요.
- 역할(roles)과 랜드마크(landmarks)에 불필요한 마크업을 추가하지 마세요. 각 컴포넌트의 가이드라인을 따르세요.
- 모든 콘텐츠에 논리적이고 예측 가능한 순서로 키보드 기능을 제공하세요. 정보의 흐름이 자연스러워야 해요.
Component-specific guidelines
Alternative text
ariaLabelledby속성을 통해 앵커 내비게이션에 의미 있는 라벨을 제공하세요.