UNPKG

@daikin-oss/tailwind

Version:

Tailwind plugin using DDS design tokens

148 lines (144 loc) 4.85 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default }); module.exports = __toCommonJS(src_exports); var import_tokens = __toESM(require("@daikin-oss/dds-tokens/json/daikin/Light/tokens.json"), 1); var import_plugin = __toESM(require("tailwindcss/plugin"), 1); // src/tokenTypes.ts function unifyTokenType(sdType, tsType) { switch (tsType) { case "borderWidth": case "borderRadius": case "sizing": case "spacing": return tsType; default: return sdType; } } // src/index.ts function createPerTypeTokenMap(keyPrefix, cssVarPrefix) { return Object.entries(import_tokens.default).reduce((acc, [key, [value, sdType, tsType]]) => { const unifiedType = unifyTokenType( sdType, tsType ); const object = acc[unifiedType] ??= {}; const varRef = `var(${cssVarPrefix}${key})`; const comment = value ? ` /* ${value} */` : ""; object[`${keyPrefix}${key}`] = `${varRef}${comment}`; return acc; }, {}); } var DEFAULT_OPTIONS = { prefix: "dds-", cssVarPrefix: "--dds-" }; var tokensPlugin = import_plugin.default.withOptions( (options) => ( // eslint-disable-next-line @typescript-eslint/unbound-method ({ addUtilities }) => { const { prefix, cssVarPrefix } = { ...DEFAULT_OPTIONS, ...options }; const perTypeTokenMap = createPerTypeTokenMap(prefix, cssVarPrefix); addUtilities( Object.entries(perTypeTokenMap.border ?? {}).map( ([key, value]) => ({ [`.b-${key}`]: { border: value }, [`.bl-${key}`]: { "border-left": value }, [`.br-${key}`]: { "border-right": value }, [`.bt-${key}`]: { "border-top": value }, [`.bb-${key}`]: { "border-bottom": value }, [`.bx-${key}`]: { "border-left": value, "border-right": value }, [`.by-${key}`]: { "border-top": value, "border-bottom": value }, [`.o-${key}`]: { outline: value } }) ) ); addUtilities( Object.entries(perTypeTokenMap.typography ?? {}).map( ([key, value]) => ({ [`.type-${key}`]: { font: value } }) ) ); } ), (options) => { const { prefix, cssVarPrefix } = { ...DEFAULT_OPTIONS, ...options }; const perTypeTokenMap = createPerTypeTokenMap(prefix, cssVarPrefix); return { theme: { extend: { colors: perTypeTokenMap.color, boxShadow: perTypeTokenMap.shadow, fontFamily: { ...perTypeTokenMap.fontFamily, // TODO: This is kept for compatibility reasons. We should migrate to design token. daikinSerif: "Roboto, sans-serif" }, fontSize: perTypeTokenMap.fontSize, fontWeight: perTypeTokenMap.fontWeight, lineHeight: perTypeTokenMap.lineHeight, spacing: { ...perTypeTokenMap.dimension, ...perTypeTokenMap.spacing, ...perTypeTokenMap.sizing }, borderRadius: perTypeTokenMap.borderRadius, borderWidth: perTypeTokenMap.borderWidth } } }; } ); var src_default = tokensPlugin;