add-navigation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdd Navigation
接入Navigation组件
Wire from into this Dune app.
Navigation@cognite/dune-industrial-components/navigationNav items: $ARGUMENTS
The component is fully controlled — the parent owns and . No state or routing assumptions live inside it.
activeIdonNavigateThe prop is optional and defaults to the Cognite vertical logo. It uses so it automatically renders black in light mode and white in dark mode.
logocurrentColor将中的组件接入当前Dune应用。
@cognite/dune-industrial-components/navigationNavigation导航项:$ARGUMENTS
该组件为完全受控组件——由父组件控制和,组件内部不包含任何状态或路由相关的预设逻辑。
activeIdonNavigatelogocurrentColorStep 1 — Check dependencies
步骤1 — 检查依赖
First, check if is installed by looking at . If not installed, run:
@cognite/dune-industrial-componentspackage.jsonbash
pnpm install "github:cognitedata/dune-industrial-components#semver:*"首先,查看确认是否已安装。如果未安装,请运行:
package.json@cognite/dune-industrial-componentsbash
pnpm install "github:cognitedata/dune-industrial-components#semver:*"Step 2 — Read the app
步骤2 — 分析应用
Read (or the main layout file) and before changing anything. Note:
src/App.tsxpackage.json- Existing routing (?)
react-router-dom - Existing dark-mode / theme hook
- Where page content currently lives
在修改代码前,先阅读(或主布局文件)和,注意以下几点:
src/App.tsxpackage.json- 现有路由方案(是否使用?)
react-router-dom - 现有的深色模式/主题钩子
- 当前页面内容的存放位置
Step 3 — Define nav items
步骤3 — 定义导航项
Create a typed array using (fall back to the app's existing pages/routes if empty):
NavItem[]$ARGUMENTSts
import type { NavItem } from '@cognite/dune-industrial-components/navigation';
const NAV_ITEMS: NavItem[] = [
{ id: 'home', label: 'Home' },
// one entry per argument / page
];使用创建一个类型化的数组(如果参数为空,则回退到应用现有页面/路由):
$ARGUMENTSNavItem[]ts
import type { NavItem } from '@cognite/dune-industrial-components/navigation';
const NAV_ITEMS: NavItem[] = [
{ id: 'home', label: 'Home' },
// 每个参数/页面对应一个条目
];Step 4 — Wire into the layout
步骤4 — 接入布局
The Navigation uses the default Cognite logo and includes a theme toggle by default.
Navigation组件默认使用Cognite logo,并自带主题切换功能。
No router (useState)
无路由场景(使用useState)
tsx
import { useState } from 'react';
import { Navigation } from '@cognite/dune-industrial-components/navigation';
export function App() {
const [activeId, setActiveId] = useState(NAV_ITEMS[0].id);
// If the app has dark mode, use the existing hook
const { isDark, toggle } = useDarkMode(); // Adjust to match app's theme hook
return (
<div className="flex min-h-screen flex-col">
<Navigation
items={NAV_ITEMS}
activeId={activeId}
onNavigate={setActiveId}
renderThemeToggle={() => (
<button
type="button"
onClick={toggle}
aria-label={isDark ? 'Switch to light mode' : 'Switch to dark mode'}
>
{isDark ? '🌙' : '☀️'}
</button>
)}
/>
<main className="flex flex-1 flex-col">{/* page content */}</main>
</div>
);
}tsx
import { useState } from 'react';
import { Navigation } from '@cognite/dune-industrial-components/navigation';
export function App() {
const [activeId, setActiveId] = useState(NAV_ITEMS[0].id);
// 如果应用已支持深色模式,请使用现有钩子
const { isDark, toggle } = useDarkMode(); // 根据应用的主题钩子调整
return (
<div className="flex min-h-screen flex-col">
<Navigation
items={NAV_ITEMS}
activeId={activeId}
onNavigate={setActiveId}
renderThemeToggle={() => (
<button
type="button"
onClick={toggle}
aria-label={isDark ? '切换到浅色模式' : '切换到深色模式'}
>
{isDark ? '🌙' : '☀️'}
</button>
)}
/>
<main className="flex flex-1 flex-col">{/* 页面内容 */}</main>
</div>
);
}With react-router-dom
结合react-router-dom场景
tsx
import { useLocation, useNavigate } from 'react-router-dom';
import { Navigation } from '@cognite/dune-industrial-components/navigation';
export function App() {
const location = useLocation();
const navigate = useNavigate();
// If the app has dark mode, use the existing hook
const { isDark, toggle } = useDarkMode(); // Adjust to match app's theme hook
// Derive activeId from path — adjust to match the app's route structure
const activeId = location.pathname.split('/')[1] || NAV_ITEMS[0].id;
return (
<div className="flex min-h-screen flex-col">
<Navigation
items={NAV_ITEMS}
activeId={activeId}
onNavigate={(id) => navigate(`/${id === NAV_ITEMS[0].id ? '' : id}`)}
renderThemeToggle={() => (
<button
type="button"
onClick={toggle}
aria-label={isDark ? 'Switch to light mode' : 'Switch to dark mode'}
>
{isDark ? '🌙' : '☀️'}
</button>
)}
/>
<main className="flex flex-1 flex-col">{/* page content */}</main>
</div>
);
}tsx
import { useLocation, useNavigate } from 'react-router-dom';
import { Navigation } from '@cognite/dune-industrial-components/navigation';
export function App() {
const location = useLocation();
const navigate = useNavigate();
// 如果应用已支持深色模式,请使用现有钩子
const { isDark, toggle } = useDarkMode(); // 根据应用的主题钩子调整
// 从路径中提取activeId — 根据应用的路由结构调整
const activeId = location.pathname.split('/')[1] || NAV_ITEMS[0].id;
return (
<div className="flex min-h-screen flex-col">
<Navigation
items={NAV_ITEMS}
activeId={activeId}
onNavigate={(id) => navigate(`/${id === NAV_ITEMS[0].id ? '' : id}`)}
renderThemeToggle={() => (
<button
type="button"
onClick={toggle}
aria-label={isDark ? '切换到浅色模式' : '切换到深色模式'}
>
{isDark ? '🌙' : '☀️'}
</button>
)}
/>
<main className="flex flex-1 flex-col">{/* 页面内容 */}</main>
</div>
);
}Optional — Custom logo
可选配置 — 自定义logo
By default, the Navigation uses the Cognite logo which automatically adapts to light/dark themes. Only add a custom logo if needed.
To use a custom logo, pass the prop. If the app has dark/light mode, swap the image based on the theme hook:
logotsx
import { Navigation } from '@cognite/dune-industrial-components/navigation';
import logoLight from '@/assets/logo-light.svg';
import logoDark from '@/assets/logo-dark.svg';
// Inside the component (whatever hook the app uses):
const { isDark } = useDarkMode();
<Navigation
items={NAV_ITEMS}
activeId={activeId}
onNavigate={setActiveId}
logo={
<img
src={isDark ? logoDark : logoLight}
alt="App"
className="h-8 w-auto"
/>
}
renderThemeToggle={() => (
<button
type="button"
onClick={toggle}
aria-label={isDark ? 'Switch to light mode' : 'Switch to dark mode'}
>
{isDark ? '🌙' : '☀️'}
</button>
)}
/>默认情况下,Navigation组件会使用适配深浅色主题的Cognite logo。仅在需要时添加自定义logo。
如需使用自定义logo,传入属性。如果应用支持深浅色模式,可根据主题钩子切换图片:
logotsx
import { Navigation } from '@cognite/dune-industrial-components/navigation';
import logoLight from '@/assets/logo-light.svg';
import logoDark from '@/assets/logo-dark.svg';
// 在组件内部(使用应用对应的钩子):
const { isDark } = useDarkMode();
<Navigation
items={NAV_ITEMS}
activeId={activeId}
onNavigate={setActiveId}
logo={
<img
src={isDark ? logoDark : logoLight}
alt="App"
className="h-8 w-auto"
/>
}
renderThemeToggle={() => (
<button
type="button"
onClick={toggle}
aria-label={isDark ? '切换到浅色模式' : '切换到深色模式'}
>
{isDark ? '🌙' : '☀️'}
</button>
)}
/>Done
完成
The app now has a sticky navigation header with:
- Default Cognite logo that adapts to light/dark themes
- Theme toggle button (if dark mode hook exists)
- on the active item
aria-current="page" - attributes on the logo button and
aria-labelelement<nav>
Package:Exports:@cognite/dune-industrial-components/navigation,Navigation,CogniteLogo,NavItemNavigationProps
现在应用已拥有一个粘性导航头部,包含以下功能:
- 自动适配深浅色主题的默认Cognite logo
- 主题切换按钮(如果应用存在深色模式钩子)
- 激活项上的属性
aria-current="page" - logo按钮和元素上的
<nav>属性aria-label
包:导出内容:@cognite/dune-industrial-components/navigation,Navigation,CogniteLogo,NavItemNavigationProps