Zephora UI

การตั้งค่า

การตั้งค่าระดับ global สำหรับทุกคอมโพเนนต์ของ Zephora — ห่อแอปของคุณหนึ่งครั้งด้วย ZephoraConfigProvider แล้วตั้งค่า locale, เลเยอร์ z-index ของ overlay, คอนเทนเนอร์ portal และค่าเริ่มต้นของ headless

การตั้งค่า

import { ZephoraConfigProvider } from "@zephora/react";

export default function App() {
  return (
    <ZephoraConfigProvider
      locale="tr"
      zIndex={{ modal: 2000, toast: 2100 }}
      appendTo={typeof document !== "undefined" ? document.body : null}
      unstyled={false}
    >
      <YourApp />
    </ZephoraConfigProvider>
  );
}

ZephoraConfigProvider props

Propชนิดค่าเริ่มต้นคำอธิบาย
localestring"en"Active locale for component UI strings. Only "en" is embedded; load others from @zephora/theme/locales/* (module or JSON) and register with addLocale().
zIndex{ dropdown?, overlay?, modal?, popover?, toast?, tooltip?: number }Overrides the layering of floating elements. Written as --z-index-* CSS variables on the root element.
appendToHTMLElement | nulldocument.bodyContainer that Portal-based overlays (Dialog, Select menus, Tooltip…) render into.
unstyledbooleanfalseMakes every component headless by default for Tailwind-first apps. A per-component unstyled prop still wins.

การอ่านค่า config

import { useZephoraConfig, useLocale } from "@zephora/react";

function MyField() {
  const config = useZephoraConfig(); // { locale, zIndex, appendTo, unstyled }
  const { t, messages, locale } = useLocale();
  return <button aria-label={t("close")} />;
}

เลเยอร์ z-index

คอมโพเนนต์กำหนดลำดับการซ้อนจาก CSS variables ที่มีค่า fallback ในตัว ดังนั้นทั้ง provider และ CSS ธรรมดาจึงปรับค่าเหล่านี้ใหม่ได้:

:root {
  --z-index-dropdown: 1000;
  --z-index-overlay: 1040;
  --z-index-modal: 1050;
  --z-index-popover: 1060;
  --z-index-toast: 1070;
  --z-index-tooltip: 1080;
}

โหมดมืด

@zephora/theme มาพร้อมตัวช่วยโหมดมืดอัตโนมัติ hook useColorScheme จะแปลงค่าเป็นสคีม (ด้วย mode: "system" จะตามค่าที่ระบบปฏิบัติการตั้งไว้แบบเรียลไทม์) และโดยค่าเริ่มต้นจะใช้ธีมที่แปลงแล้วแบบ global:

import { useColorScheme } from "@zephora/theme";

function App() {
  // mode: "light" | "dark" | "system" (default "system")
  const { scheme, theme } = useColorScheme({ mode: "system" });
  return <span>Active scheme: {scheme}</span>;
}

useColorScheme(options) props

Propชนิดค่าเริ่มต้นคำอธิบาย
mode"light" | "dark" | "system""system""system" follows the OS preference live; "light"/"dark" force a scheme.
light / darkThemelightTheme / darkThemeTheme objects used for each scheme.
applybooleantrueWrite the resolved theme to document.documentElement via applyTheme.
→ returns{ scheme: "light" | "dark"; theme: Theme }The resolved scheme and the theme object in effect.

primitive ระดับล่างก็ถูก export ออกมาด้วยเช่นกัน:

import { getSystemColorScheme, watchSystemColorScheme } from "@zephora/theme";

// Reads the OS color scheme (SSR-safe; defaults to "light").
const scheme = getSystemColorScheme(); // "light" | "dark"

// Subscribes to OS changes. Returns an unsubscribe function.
const stop = watchSystemColorScheme((next) => console.log(next));
stop();

SSR ที่ปลอด FOUC

สำหรับการเรนเดอร์ฝั่งเซิร์ฟเวอร์ colorSchemeCss(light?, dark?) จะสร้างสไตล์ชีตแบบสถิตที่สลับธีมตามค่าที่ระบบปฏิบัติการตั้งไว้ — ไม่มีการกระพริบของธีมที่ผิด ไม่ต้องใช้ JavaScript ก่อนการวาด การตั้งค่า data-zephora-scheme บน <html> จะ override media query (เชื่อมกับสวิตช์ของผู้ใช้ได้):

import { colorSchemeCss } from "@zephora/theme";

// Next.js app router — app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <style dangerouslySetInnerHTML={{ __html: colorSchemeCss() }} />
      </head>
      <body>{children}</body>
    </html>
  );
}

Slot classNames

คอมโพเนนต์แบบหลาย slot (Dialog, Sheet, Tabs, Card, Popover, Tooltip…) เปิดสัญญาคลาสแยกตาม slot: root จะรับ classNames ที่มีชนิดเป็น Partial<Record<<Name>Slot, string>> (union ของชื่อ slot เช่น DialogSlot ถูก export ออกมา) คลาสของ slot จะถูกต่อท้ายหลังคลาสของโมดูล และไม่ถูกจำกัดด้วย unstyled — ทำงานได้ทั้งในโหมดมีสไตล์และไม่มีสไตล์ ในโหมดไม่มีสไตล์คลาสเหล่านี้คือคลาสเดียวที่มี classNames.root จะรวมเข้ากับ prop className ที่มีอยู่ ซึ่งยังคงเจาะไปที่ root และถูกใช้เป็นลำดับสุดท้าย สำหรับคอมโพเนนต์แบบประกอบ (compound) classNames จะถูกตั้งครั้งเดียวที่ root แล้วส่งต่อไปยังส่วนต่าง ๆ ผ่าน context

Server components

บันเดิล @zephora/react ที่เผยแพร่มาพร้อมไดเรกทีฟ "use client" อยู่แล้ว คุณจึงสามารถ import คอมโพเนนต์เข้าไปในต้นไม้ของ React Server Component (Next.js app router) ได้โดยตรงโดยไม่ต้องเพิ่มไดเรกทีฟเอง — ทุกคอมโพเนนต์ของ Zephora เป็น client component