Skip to content

Web Fonts preset

ใช้ web fonts จาก Google Fonts, FontShare โดยเพียงให้ชื่อฟอนต์

ดู providers ทั้งหมดที่รองรับ

Source Code

การติดตั้ง

bash
pnpm add -D @unocss/preset-web-fonts
bash
yarn add -D @unocss/preset-web-fonts
bash
npm install -D @unocss/preset-web-fonts
bash
bun add -D @unocss/preset-web-fonts
ts
import presetWebFonts from '@unocss/preset-web-fonts'
import presetWind3 from '@unocss/preset-wind3'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetWind3(),
    presetWebFonts({ /* options */ }),
  ],
})

TIP

preset นี้รวมอยู่ในแพ็กเกจ unocss คุณสามารถ import จากที่นั่นได้เช่นกัน:

ts
import { presetWebFonts } from 'unocss'

Providers

Providers ที่รองรับในปัจจุบัน:

INFO

ยินดีต้อนรับ PR เพื่อเพิ่ม providers เพิ่มเติม 🙌

Custom fetch function

ใช้ฟังก์ชันของคุณเองเพื่อดึง font source

ts
import presetWebFonts from '@unocss/preset-web-fonts'
import presetWind3 from '@unocss/preset-wind3'
import axios from 'axios'
import ProxyAgent from 'proxy-agent'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetWind3(),
    presetWebFonts({
      // ใช้ axios กับ https proxy
      customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }).then(it => it.data),
      provider: 'google',
      fonts: {
        sans: 'Roboto',
        mono: ['Fira Code', 'Fira Mono:400,700'],
      },
    }),
  ],
})

Options

provider

  • Type: WebFontsProviders
  • Default: google

Provider service ของ web fonts

ts
type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none'

fonts

  • Type: Record<string, WebFontMeta | string | (WebFontMeta | string)[]>

ฟอนต์ ดู example สำหรับรายละเอียดเพิ่มเติม

ts
interface WebFontMeta {
  name: string
  weights?: (string | number)[]
  italic?: boolean
  /**
   * Override the provider
   * @default <matches root config>
   */
  provider?: WebFontsProviders
}

extendTheme

  • Type: boolean
  • Default: true

Extend theme object

themeKey

  • Type: string
  • Default: fontFamily

Key สำหรับ theme object

inlineImports

  • Type: boolean
  • Default: true

Inline CSS @import()

customFetch

  • Type: (url: string) => Promise<string>
  • Default: undefined

ใช้ฟังก์ชันของคุณเองเพื่อดึง font source ดู Custom fetch function

Example

ts
presetWebFonts({
  provider: 'google', // default provider
  fonts: {
    // เหล่านี้จะ extend default theme
    sans: 'Roboto',
    mono: ['Fira Code', 'Fira Mono:400,700'],
    // custom ones
    lobster: 'Lobster',
    lato: [
      {
        name: 'Lato',
        weights: ['400', '700'],
        italic: true,
      },
      {
        name: 'sans-serif',
        provider: 'none',
      },
    ],
  },
})

CSS ต่อไปนี้จะถูกสร้างโดยอัตโนมัติ:

css
@import url('https://fonts.googleapis.com/css2?family=Roboto&family=Fira+Code&family=Fira+Mono:wght@400;700&family=Lobster&family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap');

/* layer: default */
.font-lato {
  font-family: "Lato", sans-serif;
}
.font-lobster {
  font-family: "Lobster";
}
.font-mono {
  font-family: "Fira Code", "Fira Mono", ui-monospace, SFMono-Regular, Menlo,
    Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
.font-sans {
  font-family: "Roboto", ui-sans-serif, system-ui, -apple-system,
    BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans",
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
    "Noto Color Emoji";
}

Serve Fonts Locally

โดยค่าเริ่มต้น preset จะดึงฟอนต์จาก CDN ของ provider หากคุณต้องการให้บริการฟอนต์ในเครื่อง คุณสามารถดาวน์โหลดฟอนต์และให้บริการจาก server ของคุณเองโดยใช้ processor จาก @unocss/preset-web-fonts/local

ts
import presetWebFonts from '@unocss/preset-web-fonts'
import { createLocalFontProcessor } from '@unocss/preset-web-fonts/local'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetWebFonts({
      provider: 'none',
      fonts: {
        sans: 'Roboto',
        mono: 'Fira Code',
      },
      // นี่จะดาวน์โหลดฟอนต์และให้บริการในเครื่อง
      processors: createLocalFontProcessor({
        // Directory เพื่อ cache ฟอนต์
        cacheDir: 'node_modules/.cache/unocss/fonts',

        // Directory เพื่อบันทึก font assets
        fontAssetsDir: 'public/assets/fonts',

        // Base URL เพื่อให้บริการฟอนต์จาก client
        fontServeBaseUrl: '/assets/fonts'
      })
    }),
  ],
})

นี่จะดาวน์โหลด font assets ไปยัง public/assets/fonts และให้บริการจาก /assets/fonts บน client เมื่อทำเช่นนี้ โปรดตรวจสอบให้แน่ใจว่า license ของฟอนต์อนุญาตให้คุณแจกจ่ายซ้ำ เครื่องมือนี้ไม่รับผิดชอบต่อปัญหาทางกฎหมายใดๆ

INFO

คุณสมบัตินี้เฉพาะสำหรับ Node.js และจะไม่ทำงานใน browser

Released under the MIT License.