 
 เมื่อ 2 สัปดาห์ก่อน Tailwind CSS v4.0 เพิ่งออกมา วันนี้เลยมาลองทำการเปลี่ยนจาก Tailwind v3 มาเป็น v4 ดูครับ เนื่องจากเว็บบล็อกนี้ใช้ Astro + Tailwind ตัวอย่าง และรายละเอียด ก็จะอ้างอิงถึง Astro
 
สิ่งที่เพิ่มมาใน Tailwind v4 ที่น่าสนใจ ก็เช่น
- container query : เป็น API ที่ทำให้เราสามารถจัดการขนาด container ได้ง่ายขึ้น
- gradient API : ทำ gradient ได้ง่าย และสะดวกขึ้น
- dynamic values : อันนี้ชอบมาก คือ เราสามารถปรับ spacing, grid หรืออะไรได้สะดวก โดยไม่ต้องอ้างอิงตามชื่อ class แล้ว เช่น grid-cols-16,grid-cols-20
- ติดตั้งง่ายขึ้นมาก เพียงแค่ 3 ขั้นตอน ดังนี้
- ติดตั้ง Tailwind CSS
npm install tailwindcss @tailwindcss/postcss- เพิ่ม plugins
export default {  plugins: ['@tailwindcss/postcss']}- ทำการ import CSS
@import 'tailwindcss';- Automatic content detection: ไม่ต้องมากำหนด contentใน config file แล้ว ว่าจะให้ tailwind scan ไฟล์ไหนบ้าง ตัว tailwind จะ scan ไฟล์ให้เอง โดย ignore ตาม.gitignore
- ไฟล์ config ด้วย CSS - ก่อนหน้านี้เราจะใช้ไฟล์ tailwind.config.jsแต่ว่า v4 เราจะ custom เป็น CSS ได้เลย ตัวอย่าง
@import 'tailwindcss';@theme {  --font-display: 'Satoshi', 'sans-serif';  --breakpoint-3xl: 1920px;  --color-avocado-100: oklch(0.99 0 0);  --color-avocado-200: oklch(0.98 0.04 113.22);  --color-avocado-300: oklch(0.94 0.11 115.03);  --color-avocado-400: oklch(0.92 0.19 114.08);  --color-avocado-500: oklch(0.84 0.18 117.33);  --color-avocado-600: oklch(0.53 0.12 118.34);  --ease-fluid: cubic-bezier(0.3, 0, 0, 1);  --ease-snappy: cubic-bezier(0.2, 0, 0, 1);  /* ... */}Upgrade Tailwind v4 Astro
ในโปรเจ็ค Astro ผมทำการ ติดตั้ง และอัพเกรดดังนี้ ตัว Astro ใช้ Tailwind ผ่าน Vite
npm i tailwindcss @tailwindcss/viteที่ไฟล์ astro.config.mjs ทำการ เพิ่ม plugins ไปแทนที่ tailwind ใน integration
import tailwindcss from '@tailwindcss/vite'import tailwind from '@astrojs/tailwind'
export default defineConfig({  vite: {    plugins: [tailwindcss()]  },  integrations: [    tailwind()  ]})เปลี่ยนจาก การใช้ @tailwind เป็น @import
@tailwind base;@tailwind components;@tailwind utilities;
@import "tailwindcss";@import "./theme.css";มีบางไฟล์ .astro ที่ใช้ การกำหนด style แบบ scoped ก็เลยต้องอ้างอิง reference ให้มัน ไม่งั้น Tailwind จะไม่รู้จัก utility class นั้นๆ
<style scoped>  @reference '../styles/custom.css';
  .link {    @apply text-primary-600;  }</style>Plugin typography
เนื่องจากเราได้ใช้ตัว plugin @typograhpy ด้วย เราก็สามารถ custom ผ่านไฟล์ global.css ของเราได้เลย
@import 'tailwindcss';@plugin "@tailwindcss/typography";แต่เนื่องจากว่า มีการปรับแต่ง เพิ่มเติมจาก tailwind.config.js ตัวอย่าง เช่น
import colors from 'tailwindcss/colors'
/** @type {import('tailwindcss').Config} */export default {  content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'],  darkMode: 'class',  theme: {    extend: {      colors: {        primary: colors.blue,        secondary: colors.rose,        gray: colors.slate      },      typography: ({ theme }) => ({        DEFAULT: {          css: {            ':not(pre) > code': {              color: `${theme('colors.rose.600')}`,              backgroundColor: `${theme('colors.gray.100')}`            },          }        },        invert: {          css: {            color: theme('colors.slate.200'),            ':not(pre) > code': {              color: `${theme('colors.pink.300')}`,              backgroundColor: `${theme('colors.gray.800')}`            },            h1: {              fontWeight: '600',              letterSpacing: theme('letterSpacing.wide'),              color: theme('colors.slate.100')            }        }      })    }  },  plugins: [require('@tailwindcss/forms'), require('@tailwindcss/typography')]}เราก็เลยใช้วิธี import tailwind.config.js ไปไว้ใน css ไฟล์แบบนี้
@import 'tailwindcss';@config "../../tailwind.config.js";การปรับแต่ง theme
เราสามารถดู Default Theme ของ Tailwind v4 ได้ที่นี่ เราจะ custom โดยการตั้งชื่อไฟล์ใหม่ theme.css
@theme {  --font-mono:    ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New',    monospace;
  --color-primary-500: blue;}จากนั้น ก็ import ไว้ที่ไฟล์ css หลักเรา
@import 'tailwindcss';@import './theme.css';
@config "../../tailwind.config.mjs";เบื้องต้น ตัวเว็บก็ทำการ migrate มา Tailwind v4 เรียบร้อยแล้ว ลองทดสอบ และใช้งานที่เคยเขียน พบว่า ไม่เจอโค๊ดที่มีปัญหาครับ และก็พร้อมที่จะเริ่มใช้งาน Tailwind v4 แล้ว และคิดว่าเร็วๆ นี้น่าจะได้ใช้พวก container query, dynamic values และก็ gradient APIs ครับ
- Authors
-    Chai Phonbopitเป็น Web Dev ในบริษัทแห่งหนึ่ง ทำงานมา 10 ปีกว่าๆ ด้วยภาษาและเทคโนโลยี เช่น JavaScript, Node.js, React, Vue และปัจจุบันกำลังสนใจในเรื่องของ Blockchain และ Crypto กำลังหัดเรียนภาษา Rust 
