字體排印
styled 套件預設使用系統字體堆疊:
--rdrp-font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;這會在各個平台上使用作業系統的原生字體進行渲染(macOS 上的 San Francisco,Windows 上的 Segoe UI,Android 上的 Roboto)。系統字體提供了良好的多語言支援,因為作業系統會自動處理中日韓、阿拉伯文和其他腳本的備用字體。
若要覆寫,請在任何祖先元素上設定 --rdrp-font-family — 它會串聯至選擇器中。
CSS 變數行內樣式
透過在包覆元素上使用 style 屬性來覆寫字體:
import { useState } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
function CSSCustomFont() { const [value, setValue] = useState<Date | null>(null);
return ( <div style={{ ["--rdrp-font-family" as string]: '"Georgia", "Times New Roman", serif', }} > <DatePicker value={value} onChange={setValue} /> </div> );}字體家族
透過在包覆元素上設定 --rdrp-font-family 來切換字體家族:
import { useState } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
const fonts = { System: {}, Serif: { "--rdrp-font-family": '"Georgia", "Times New Roman", serif' }, Mono: { "--rdrp-font-family": '"Menlo", "Courier New", monospace' },};
type FontName = keyof typeof fonts;
function CompoundCustomFont() { const [value, setValue] = useState<Date | null>(null); const [font, setFont] = useState<FontName>("System");
return ( <div> <div style={{ display: "flex", gap: 6, marginBottom: 12 }}> {(Object.keys(fonts) as FontName[]).map((name) => ( <button key={name} onClick={() => setFont(name)} style={{ padding: "6px 14px", border: `1px solid ${font === name ? "#3b82f6" : "#d1d5db"}`, borderRadius: 6, background: font === name ? "#eff6ff" : "#fff", cursor: "pointer", fontSize: 13, }} > {name} </button> ))} </div> <div style={fonts[font] as React.CSSProperties}> <DatePicker value={value} onChange={setValue} inline /> </div> </div> );}March 2026
Su
Mo
Tu
We
Th
Fr
Sa
使用網頁字體 (Noto Sans)
對於需要一致跨平台渲染或國際化 (i18n) 的專案,可以載入像 Noto Sans 這樣的網頁字體並覆寫該變數:
import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
// 1. 在您的 HTML 或 CSS 中載入 Noto Sans:// <link href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
// 2. 覆寫字體變數:<div style={{ "--rdrp-font-family": '"Noto Sans", sans-serif' } as React.CSSProperties}> <DatePicker value={value} onChange={setValue} inline /></div>;import { useState, useEffect } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
function CompoundNotoSans() { const [value, setValue] = useState<Date | null>(null);
useEffect(() => { // Load Noto Sans from Google Fonts (for demo purposes only) if (!document.getElementById("noto-sans-font")) { const link = document.createElement("link"); link.id = "noto-sans-font"; link.rel = "stylesheet"; link.href = "https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;500;600;700&display=swap"; document.head.appendChild(link); } }, []);
return ( <div style={{ "--rdrp-font-family": '"Noto Sans", sans-serif' } as React.CSSProperties}> <DatePicker value={value} onChange={setValue} inline /> </div> );}March 2026
Su
Mo
Tu
We
Th
Fr
Sa
網頁字體展示
比較不同的網頁字體,看看字體排印如何顯著改變選擇器的外觀。以下所有字體都支援多語言渲染(拉丁文、中日韓、韓文)。
⚠️ 僅供示範
以下字體是從 Google Fonts CDN 載入以供示範。此函式庫不包含任何網頁字體 — 您需要在自己的專案中載入它們。
import { useState, useEffect } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
const WEB_FONTS = [ { name: "Noto Sans", family: '"Noto Sans", sans-serif' }, { name: "Noto Serif", family: '"Noto Serif", serif' }, { name: "IBM Plex Sans", family: '"IBM Plex Sans", sans-serif' }, { name: "Roboto Slab", family: '"Roboto Slab", serif' },];
const FONT_URL = "https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600;700&family=Noto+Sans:wght@400;500;600;700&family=Noto+Serif:wght@400;500;600;700&family=Roboto+Slab:wght@400;500;600;700&display=swap";
function CompoundWebFonts() { const [value, setValue] = useState<Date | null>(null); const [active, setActive] = useState(0);
useEffect(() => { if (!document.getElementById("web-fonts-link")) { const link = document.createElement("link"); link.id = "web-fonts-link"; link.rel = "stylesheet"; link.href = FONT_URL; document.head.appendChild(link); } }, []);
return ( <div> <div style={{ display: "flex", gap: 6, marginBottom: 12, flexWrap: "wrap" }}> {WEB_FONTS.map((f, i) => ( <button key={f.name} onClick={() => setActive(i)} style={{ padding: "6px 14px", border: `1px solid ${active === i ? "#3b82f6" : "#d1d5db"}`, borderRadius: 6, background: active === i ? "#eff6ff" : "#fff", color: active === i ? "#3b82f6" : "#374151", cursor: "pointer", fontSize: 13, fontFamily: f.family, }} > {f.name} </button> ))} </div> <div style={{ "--rdrp-font-family": WEB_FONTS[active].family } as React.CSSProperties}> <DatePicker value={value} onChange={setValue} inline /> </div> </div> );}March 2026
Su
Mo
Tu
We
Th
Fr
Sa
字體大小與字重
透過覆寫大小和字重變數來微調字體排印的密度:
| 變數 | 預設值 | 描述 |
|---|---|---|
--rdrp-font-size-xs | 11px | 最小的文字 |
--rdrp-font-size-sm | 12px | 星期標籤 |
--rdrp-font-size-base | 13px | 日期儲存格、按鈕 |
--rdrp-font-size-md | 14px | 觸發器、時間項目 |
--rdrp-font-size-lg | 15px | 月份標題 |
--rdrp-font-weight-normal | 400 | 基本字重 |
--rdrp-font-weight-medium | 500 | 中等強調 |
--rdrp-font-weight-semibold | 600 | 已選日期、標題 |
--rdrp-font-weight-bold | 700 | 今日指示器 |
import { useState } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
const presets = { Compact: { "--rdrp-font-size-xs": "9px", "--rdrp-font-size-sm": "10px", "--rdrp-font-size-base": "11px", "--rdrp-font-size-md": "12px", "--rdrp-font-size-lg": "13px", "--rdrp-font-weight-semibold": "500", "--rdrp-font-weight-bold": "600", }, Default: {}, Large: { "--rdrp-font-size-xs": "13px", "--rdrp-font-size-sm": "14px", "--rdrp-font-size-base": "15px", "--rdrp-font-size-md": "16px", "--rdrp-font-size-lg": "18px", "--rdrp-font-weight-semibold": "700", "--rdrp-font-weight-bold": "800", },};
type PresetName = keyof typeof presets;
function CompoundCustomFontPopup() { const [value, setValue] = useState<Date | null>(null); const [preset, setPreset] = useState<PresetName>("Default");
return ( <div> <div style={{ display: "flex", gap: 6, marginBottom: 12 }}> {(Object.keys(presets) as PresetName[]).map((name) => ( <button key={name} onClick={() => setPreset(name)} style={{ padding: "6px 14px", border: `1px solid ${preset === name ? "#3b82f6" : "#d1d5db"}`, borderRadius: 6, background: preset === name ? "#eff6ff" : "#fff", cursor: "pointer", fontSize: 13, }} > {name} </button> ))} </div> <div style={presets[preset] as React.CSSProperties}> <DatePicker value={value} onChange={setValue} inline /> </div> </div> );}March 2026
Su
Mo
Tu
We
Th
Fr
Sa