Heading
<ui-heading> สร้างหัวข้อที่ระดับถูกต้องโดยไม่ต้องนับเอง: <ui-section> แต่ละชั้นที่ครอบอยู่ทำให้ลึกลงหนึ่งระดับ markup เดียวกันจึงเป็น <h2> ในหน้าหนึ่ง และเป็น <h4> ใน panel ที่ซ้อนอยู่ ตัวอักษรเป็นไปตาม productive type scale ของ Carbon สำหรับระดับนั้น
สัญญา CT-2026-0142
คู่สัญญา
ผู้ลงนาม
ข้อมูลติดต่อ
HTMLReactReact + Tailwind<ui-section level="3">
<ui-heading>สัญญา CT-2026-0142</ui-heading>
<ui-section>
<ui-heading>คู่สัญญา</ui-heading>
<ui-section>
<ui-heading>ผู้ลงนาม</ui-heading>
<ui-section>
<ui-heading>ข้อมูลติดต่อ</ui-heading>
</ui-section>
</ui-section>
</ui-section>
</ui-section>
<UiSection level={3}>
<UiHeading>สัญญา CT-2026-0142</UiHeading>
<UiSection>
<UiHeading>คู่สัญญา</UiHeading>
<UiSection>
<UiHeading>ผู้ลงนาม</UiHeading>
<UiSection>
<UiHeading>ข้อมูลติดต่อ</UiHeading>
</UiSection>
</UiSection>
</UiSection>
</UiSection>
<Section level={3}>
<Heading>สัญญา CT-2026-0142</Heading>
<Section>
<Heading>คู่สัญญา</Heading>
<Section>
<Heading>ผู้ลงนาม</Heading>
<Section>
<Heading>ข้อมูลติดต่อ</Heading>
</Section>
</Section>
</Section>
</Section>
ตัวอย่างในหน้านี้เริ่มที่ระดับ 3 เพราะหน้านี้มี <h1> และ <h2> ของตัวเองแล้ว ผลด้านบนจึงเป็น <h3> <h4> <h5> <h6>
ติดตั้ง
npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/heading
npx shadcn@4 add @kanok-ui/tw-heading
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/heading.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/heading/heading-shared.ts
# components/ui/primitives/heading/heading.ts
# components/ui/primitives/heading/section.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# shared-element, tokens
# React + Tailwind
# 1. ติดตั้ง dependency
# (ไม่มี)
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-heading.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/heading.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
<ui-heading> ที่ไม่อยู่ใน section ใดเลยเป็น <h1> section ชั้นแรกทำให้หัวข้อข้างในเป็น <h2> section ที่ซ้อนอยู่ข้างในเป็น <h3> ต่อไปเรื่อย ๆ จนถึง <h6>
HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/heading';</script>
<ui-heading>สัญญา</ui-heading>
<ui-section>
<ui-heading>รออนุมัติ</ui-heading>
</ui-section>
import { UiHeading, UiSection } from '@purpurabog/ui/react';
<UiHeading>สัญญา</UiHeading>
<UiSection>
<UiHeading>รออนุมัติ</UiHeading>
</UiSection>
import { Heading, Section } from '@/components/ui/react-tw/heading';
<Heading>สัญญา</Heading>
<Section>
<Heading>รออนุมัติ</Heading>
</Section>
<ui-section> เป็นแค่ block ไม่มี role ส่วน Section ของ React + Tailwind สร้าง element <section> เปลี่ยนได้ด้วย as (เช่น as="div")
กำหนดระดับเอง
level บน section กำหนดระดับของหัวข้อข้างในโดยตรง แทนที่จะเป็นหนึ่งระดับลึกกว่า section ที่ครอบ ใช้เมื่อ block หนึ่งเริ่มอยู่ใต้หัวข้อที่ไม่ใช่ <ui-heading> เช่น ชื่อหน้าจาก layout ของคุณ section ที่ซ้อนข้างในจะนับต่อจากนั้น ระดับสูงสุดคือ 6
ประวัติการอนุมัติ
ฝ่ายกฎหมายตรวจ 3 ตุลาคม
HTMLReactReact + Tailwind<ui-section level="4">
<ui-heading>ประวัติการอนุมัติ</ui-heading>
<ui-section>
<ui-heading>ฝ่ายกฎหมายตรวจ 3 ตุลาคม</ui-heading>
</ui-section>
</ui-section>
<UiSection level={4}>
<UiHeading>ประวัติการอนุมัติ</UiHeading>
<UiSection>
<UiHeading>ฝ่ายกฎหมายตรวจ 3 ตุลาคม</UiHeading>
</UiSection>
</UiSection>
<Section level={4}>
<Heading>ประวัติการอนุมัติ</Heading>
<Section>
<Heading>ฝ่ายกฎหมายตรวจ 3 ตุลาคม</Heading>
</Section>
</Section>
ตัวอักษรของแต่ละระดับ
| ระดับ |
Type ของ Carbon |
ขนาด |
<h1> |
heading-06 |
42px ตัวบาง |
<h2> |
heading-05 |
32px |
<h3> |
heading-04 |
28px |
<h4> |
heading-03 |
20px |
<h5> |
heading-02 |
16px ตัวหนาปานกลาง |
<h6> |
heading-01 |
14px ตัวหนาปานกลาง |
หัวข้อไม่มี margin ให้เว้นระยะด้วย layout รอบ ๆ เช่น gap ของ flex box หรือของ grid
block เดียวกันที่ความลึกใดก็ได้
block ที่ใช้ซ้ำ เช่น การ์ดสรุป ใส่ <ui-heading> ไว้ได้และถูกต้องเสมอไม่ว่าจะวางไว้ที่ไหน: อยู่ใน section ชั้นเดียวเป็น <h4> อยู่ในสองชั้นเป็น <h5>
งบประมาณของแผนก
ฝ่ายกฎหมาย
ใช้ไป 1.2 ล้านจาก 2.0 ล้านบาท
ที่ปรึกษากฎหมายภายนอก
ใช้ไป 4 แสนจาก 6 แสนบาท
HTMLReactReact + Tailwind<ui-section level="3">
<ui-heading>งบประมาณของแผนก</ui-heading>
<ui-section>
<ui-heading>ฝ่ายกฎหมาย</ui-heading>
<p>ใช้ไป 1.2 ล้านจาก 2.0 ล้านบาท</p>
<ui-section>
<ui-heading>ที่ปรึกษากฎหมายภายนอก</ui-heading>
<p>ใช้ไป 4 แสนจาก 6 แสนบาท</p>
</ui-section>
</ui-section>
</ui-section>
function BudgetCard({ name, spent, children }: { name: string; spent: string; children?: React.ReactNode }) {
return (
<UiSection>
<UiHeading>{name}</UiHeading>
<p>{spent}</p>
{children}
</UiSection>
);
}
<UiSection level={3}>
<UiHeading>งบประมาณของแผนก</UiHeading>
<BudgetCard name="ฝ่ายกฎหมาย" spent="ใช้ไป 1.2 ล้านจาก 2.0 ล้านบาท">
<BudgetCard name="ที่ปรึกษากฎหมายภายนอก" spent="ใช้ไป 4 แสนจาก 6 แสนบาท" />
</BudgetCard>
</UiSection>
function BudgetCard({ name, spent, children }: { name: string; spent: string; children?: React.ReactNode }) {
return (
<Section>
<Heading>{name}</Heading>
<p>{spent}</p>
{children}
</Section>
);
}
<Section level={3}>
<Heading>งบประมาณของแผนก</Heading>
<BudgetCard name="ฝ่ายกฎหมาย" spent="ใช้ไป 1.2 ล้านจาก 2.0 ล้านบาท">
<BudgetCard name="ที่ปรึกษากฎหมายภายนอก" spent="ใช้ไป 4 แสนจาก 6 แสนบาท" />
</BudgetCard>
</Section>
web component คิดระดับของตัวเองตอนถูกใส่ลงหน้า ถ้าย้าย <ui-heading> ไปอยู่ section อื่น จะได้ระดับใหม่ตอนนั้น แต่ถ้าเปลี่ยน level ของ section ทีหลัง หัวข้อที่อยู่ข้างในแล้วจะไม่นับใหม่
แต่ง style หัวข้อ
web component เปิด <h1>…<h6> ของตัวเองเป็น part ชื่อ heading ใน React + Tailwind ใส่ className
ครบกำหนดสัปดาห์นี้
HTMLReactReact + Tailwind<style>
.heading-accent::part(heading) { color: var(--ui-link); }
</style>
<ui-section level="4">
<ui-heading class="heading-accent">ครบกำหนดสัปดาห์นี้</ui-heading>
</ui-section>
// .heading-accent::part(heading) { color: var(--ui-link); }
<UiSection level={4}>
<UiHeading className="heading-accent">ครบกำหนดสัปดาห์นี้</UiHeading>
</UiSection>
<Section level={4}>
<Heading className="text-link">ครบกำหนดสัปดาห์นี้</Heading>
</Section>
Accessibility
- ผู้ใช้ screen reader กระโดดไปตามหัวข้อและเรียกดูรายการหัวข้อเพื่อหาทาง ระดับที่เป็นไปตามการซ้อนทำให้รายการนั้นตรงกับหน้า
- ให้มี
<h1> หน้าละตัว ถ้าชื่อหน้ามาจากที่อื่น ให้ block เริ่มที่ level ลึกกว่า
<ui-section> ไม่เพิ่ม landmark ถ้าส่วนใดของหน้าต้องมี ให้ครอบด้วย <section aria-labelledby> หรือ <nav> ของคุณเอง
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-heading> · React UiHeading
Property
| Property |
ชนิด |
คำอธิบาย |
level |
HeadingLevel |
The level from the <ui-section> around it (1 outside any). |
Method
| Method |
คำอธิบาย |
syncLevel() |
Takes the level of the section around it (also called by a section whose level changed). |
Slot
| Slot |
คำอธิบาย |
| (ค่าเริ่มต้น) |
The heading text |
CSS part
| Part |
คำอธิบาย |
heading |
The <h1>…<h6> |
<ui-section> · React UiSection
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
level |
level |
number | undefined |
|
The heading level inside, instead of one more than the section around it. |
Method
| Method |
คำอธิบาย |
getCurrentLevel() |
The level its headings use (Carbon getCurrentLevel). |
syncLevel() |
Takes its level from level, else from the section around it (also called by an ancestor whose level changed). |
Slot
| Slot |
คำอธิบาย |
| (ค่าเริ่มต้น) |
The section's content |