@nulogy/tokens
v6.2.0
Published
Design tokens for the Nulogy Design System - http://nulogy.design
Downloads
1,378
Keywords
Readme
@nulogy/tokens
Nulogy's design tokens. This package processes token definitions into various formats (CSS variables, JS constants).
What are tokens?
Design tokens are the visual design atoms of the design system — specifically, they are named entities that store visual design attributes. We use them in place of hard-coded values (such as hex values for color or pixel values for spacing) in order to maintain a scalable and consistent visual system for UI development.
📦 Installation
This package is primarily consumed by other Nulogy Design System packages like @nulogy/components and @nulogy/css. Direct installation into applications is generally not required.
However, if you need direct access, you can install it via pnpm:
pnpm add @nulogy/tokens
✨ Generated Files
The build process generates the following files in the output/ directory (note: this directory is gitignored, the files are published to npm from dist/ after build and postbuild steps):
nds_tokens.js: JavaScript constants (ES Modules).nds_tokens.css: CSS custom properties (variables).nds_tokens.ts: TypeScript source identical tonds_tokens.js, used for generating type declarations.nds_tokens.d.ts: TypeScript declaration file, generated by thepostbuildscript.
💻 Development
Available commands:
pnpm build: Cleans thedist/directory and generates token files (.js,.css,.ts) in theoutput/directory. It then runs thepostbuildscript.pnpm clean: Removes thedist/directory.pnpm test: Runs tests usingvitest.pnpm format:check: Checks code formatting usingprettier.pnpm format:fix: Automatically fixes code formatting issues usingprettier.pnpm postbuild: Copies generated files fromoutput/todist/and generates TypeScript declaration files (.d.ts) indist/.
The core build logic resides in src/build/.
main.ts: Entry point for the build script.generate.ts: Reads token definition files, processes them for different devices (desktop, tablet, phone), and coordinates writing output files.format.ts: Contains functions to format the raw token data into CSS variables and JavaScript exports, including appropriate headers and variable naming conventions (e.g.,--nds-desktop-color-bluefor CSS,DESKTOP_COLOR_BLUEfor JS).constants.ts: Defines constants used during the build, such as device names and base units.
✏️ Adding or Editing Tokens
Tokens are defined in separate modules within the src/tokens/ directory. Each sub-directory represents a category of tokens (e.g., color, spacing, typography).
To add or modify tokens:
Locate or Create the Category File: Find the relevant file within a subdirectory in
src/tokens/(e.g.,src/tokens/color/index.ts). If adding a new category, create a new subdirectory and anindex.tsfile within it. This is an important step, as the build process will look for theindex.tsas the entry point for the category.Define Tokens: Token files export a default function that accepts a
baseUnitargument and returns an object containing the token definitions. The structure of this object determines the naming of the generated variables. Nested objects create hierarchical names.Example (
src/tokens/color/index.ts):// src/tokens/color/index.ts export default (_baseUnit: number) => ({ primary: { blue: '#0E7FAE', // Generates DESKTOP_COLOR_PRIMARY_BLUE, --nds-desktop-color-primary-blue, etc. darkBlue: '#004361', }, neutral: { 100: '#FFFFFF', 200: '#F8F8F8', }, // ... other colors })Naming Convention:
- The folder name (e.g.,
color) becomes the middle part of the variable name. - Keys in the returned object (e.g.,
primary,blue,neutral,100) are joined to form the rest of the variable name. - The build script automatically prefixes names with the device (
desktop,tablet,phone) and converts the final name toSNAKE_CASEfor JS andkebab-casefor CSS.
- The folder name (e.g.,
Run Build: After making changes, run
pnpm buildto regenerate the token files in theoutput/directory.Verify Changes: Check the generated files in
output/ordist/(specificallynds_tokens.cssandnds_tokens.js) to ensure the new or modified tokens appear correctly.Commit: Commit the changes to the token definition file(s) in
src/tokens/. Do not commit the generated files inoutput/ordist/. The files indist/will be updated during the release process.
