Cookie 同意橫幅
Cookie 同意橫幅+偏好設定彈窗的 React 元件:必要/分析/行銷分類開關、全接受/全拒絕、同意狀態物件回呼與 localStorage 記憶,SSR 安全且支援 reduced-motion。
每個正式網站都需要的合規元件。底部橫幅延遲滑入,提供「全部接受/全部拒絕/管理偏好」三個入口;「管理偏好」開啟置中彈窗,逐分類切換開關(必要分類鎖定為開啟),儲存後把同意物件透過 onConsent 回呼並寫入 localStorage 記憶——已有紀錄的訪客下次進站不會再被打擾。首次渲染一律不輸出,掛載後才讀取 localStorage,SSR 與 hydration 完全一致。
把這個效果裝進你的專案
在專案資料夾執行指令,CLI 會加入原始碼與依賴套件,再依下方使用範例引用元件。
npx webberui@latest add cookie-consent-bannerPlayground
即時調整 props、程式碼片段同步更新——直接試出你要的樣子再複製。
<CookieConsentBanner/>
完整範例包含預覽的排版與資料。先安裝元件,再把範例檔貼入你的專案。
安裝
npx webberui@latest add https://webberui.com/r/cookie-consent-banner.json或在 components.json 設定 registries 後,改用 @webberui/cookie-consent-banner 安裝。
使用
import {
CookieConsentBanner,
getStoredConsent,
} from "@/components/ui/cookie-consent-banner";
<CookieConsentBanner
privacyHref="/privacy"
onConsent={(consent, action) => {
// 依同意結果決定是否載入分析/行銷腳本
if (consent.analytics) {
// initAnalytics();
}
}}
/>;
// 之後任何時刻都能讀回同意紀錄(例如條件式載入追蹤程式)
const stored = getStoredConsent(); // StoredConsent | nullProps
| Prop | 型別 | 預設值 | 說明 |
|---|---|---|---|
categories | CookieCategory[] | 內建三類 | Cookie 分類清單;預設提供必要(鎖定)/分析/行銷三類繁中文案 |
onConsent | (consent: Record<string, boolean>, action: ConsentAction) => void | — | 使用者做出決定時回呼:同意物件(分類 id → 是否同意)與動作來源 |
storageKey | string | "webberui:cookie-consent" | localStorage 鍵名 |
persist | boolean | true | 是否讀寫 localStorage;設 false 時每次掛載都重新顯示(展示或自管儲存用) |
position | "bottom" | "bottom-left" | "bottom-right" | "bottom" | 橫幅停靠位置:置中橫幅或左右下角卡片 |
privacyHref | string | — | 隱私權政策連結網址;提供時說明文字後方會附上連結 |
message | string | — | 橫幅主說明文案(labels.message 的捷徑) |
showRejectAll | boolean | true | 是否顯示「全部拒絕」按鈕 |
labels | CookieConsentLabels | 內建繁中文案 | 覆寫介面文案,未提供的欄位沿用預設 |
defaultOpen | boolean | — | 強制顯示或隱藏橫幅(展示用);不提供時依 localStorage 有無紀錄決定 |
container | HTMLElement | null | — | 展示用容器:提供時橫幅與彈窗以 absolute 掛在該容器內(容器需 relative),未提供則以 fixed 掛到 document.body |
className | string | — | 透傳到橫幅根元素 |
CookieCategory
| 欄位 | 型別 | 預設值 | 說明 |
|---|---|---|---|
id | string | — | 分類識別碼,會成為同意物件的 key |
name | string | — | 分類顯示名稱 |
description | string | — | 分類用途說明 |
required | boolean | false | 必要分類:開關鎖定為開啟,任何動作下都視為同意 |
CookieConsentLabels
| 欄位 | 型別 | 預設值 | 說明 |
|---|---|---|---|
title | string | "Cookie 使用說明" | 橫幅標題 |
message | string | 內建說明文案 | 橫幅主說明文案 |
privacyText | string | "隱私權政策" | 隱私權連結文字 |
acceptAll | string | "全部接受" | 「全部接受」按鈕文字 |
rejectAll | string | "全部拒絕" | 「全部拒絕」按鈕文字 |
manage | string | "管理偏好" | 「管理偏好」按鈕文字 |
save | string | "儲存設定" | 偏好彈窗的儲存按鈕文字 |
dialogTitle | string | "Cookie 偏好設定" | 偏好彈窗標題 |
requiredBadge | string | "必要" | 必要分類的徽章文字 |
close | string | "關閉偏好設定" | 彈窗關閉按鈕的無障礙名稱 |
另外具名匯出 getStoredConsent(storageKey?) 與 clearStoredConsent(storageKey?) 兩個輔助函式,以及 ConsentAction("accept-all" \| "reject-all" \| "custom")、StoredConsent(含 consent、action、updatedAt 三欄的儲存格式)、CookieConsentPosition 型別,方便在應用層讀取、清除與判斷同意紀錄。
細節
- 橫幅延遲約 0.6 秒才滑入——讓頁面主要內容先完成進場,同意提示不搶第一眼
- 首次渲染(含 SSR)一律不輸出任何內容,掛載後才讀取 localStorage 判斷顯示與否,避免 hydration 不一致
- localStorage 的讀寫只發生在 effect 與事件內;儲存格式為
StoredConsentJSON(同意物件、動作來源、ISO 時間),localStorage 不可用(如無痕模式)時仍照常回呼onConsent,交由呼叫端自行儲存 - 「全部拒絕」與「儲存設定」對必要分類一律記為同意;非必要分類在彈窗中預設關閉,符合「未同意前不追蹤」原則
container是展示情境的逃生口:文件頁沙盒、Storybook 都能把橫幅關進固定高度的容器內;正式使用不需要傳
可及性
- 橫幅為
role="region"並以標題命名,內含的三個動作皆為原生button - 偏好彈窗為
role="dialog"加aria-modal="true":開啟時焦點移入彈窗、Tab 在彈窗內循環,按 Esc 或點遮罩關閉並把焦點還給「管理偏好」按鈕 - 分類開關為
role="switch"加aria-checked;必要分類設為disabled且鎖定為開啟,並以徽章標示 - 使用者系統開啟「減少動態效果」時,滑入/滑出與彈窗縮放動畫改為快速淡入淡出,功能完全不變