@salty-css/react 0.1.0-alpha.25 → 0.1.0-alpha.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +127 -0
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -57,6 +57,8 @@ To get help with problems, [Join Salty CSS Discord server](https://discord.gg/R6
57
57
  - [defineVariables](#variables) - create CSS variables (tokens) that can be used in any styling function
58
58
  - [defineMediaQuery](#media-queries) - create CSS media queries and use them in any styling function
59
59
  - [defineTemplates](#templates) - create reusable templates that can be applied when same styles are used over and over again
60
+ - [defineFont](#custom-fonts) - register custom fonts via `@font-face` (or a remote stylesheet) and expose them as a CSS variable
61
+ - [defineImport](#importing-additional-css) - pull in external CSS files (relative, public, node_modules, or URL)
60
62
  - [keyframes](#keyframes-animations) - create CSS keyframes animation that can be used and imported in any styling function
61
63
 
62
64
  ### Styling helpers & utility
@@ -319,6 +321,131 @@ Example usage:
319
321
  styled('div', { base: { textStyle: 'headline.large', card: '20px' } });
320
322
  ```
321
323
 
324
+ ## Custom fonts
325
+
326
+ Register custom fonts that will be emitted as `@font-face` declarations and exposed as a CSS variable. Mirrors the developer experience of Next.js / Astro font loaders, but generated at build time alongside the rest of your Salty CSS output.
327
+
328
+ The returned object stringifies to its `font-family` value and exposes helpers for explicit usage:
329
+
330
+ - `Font.variable` → CSS variable name (e.g. `--font-inter`)
331
+ - `Font.fontFamily` → final `font-family` string with fallbacks
332
+ - `Font.className` → class that sets the variable + applies the font on a subtree
333
+ - `Font.style` → object you can spread on a React `style` prop
334
+
335
+ ```ts
336
+ // /styles/fonts.css.ts
337
+ import { defineFont } from '@salty-css/core/factories';
338
+
339
+ // 1. Local or self-hosted @font-face sources.
340
+ // URLs are passed through as-is (use a public-folder path or a CDN URL).
341
+ export const Inter = defineFont({
342
+ name: 'Inter', // CSS font-family value
343
+ variable: '--font-inter', // Optional — accepts 'font-inter' too; if omitted we derive `--font-<name>-<hash>` from the inputs
344
+ display: 'swap', // Optional default applied to variants without their own `display`
345
+ fallback: ['system-ui', 'sans-serif'], // Optional family fallbacks appended after `name`
346
+ variants: [
347
+ {
348
+ weight: 400,
349
+ style: 'normal',
350
+ // Shorthand: pass a string and the `format()` descriptor is auto-detected
351
+ // from the file extension (woff2, woff, ttf, otf, eot, svg, ttc).
352
+ src: '/fonts/inter-400.woff2',
353
+ },
354
+ {
355
+ weight: 700,
356
+ style: 'normal',
357
+ // Multiple sources can be a string array — first entry is preferred;
358
+ // the browser picks the first format it supports.
359
+ src: ['/fonts/inter-700.woff2', '/fonts/inter-700.ttf'],
360
+ },
361
+ {
362
+ weight: 400,
363
+ style: 'italic',
364
+ // Use the `{ url, format }` object form when the URL has no recognisable
365
+ // extension (signed CDN URLs, query-only endpoints, etc.). You can also
366
+ // mix strings and objects in the same array.
367
+ src: ['/fonts/inter-400-italic.woff2', { url: 'https://cdn.example.com/inter-italic', format: 'woff' }],
368
+ },
369
+ ],
370
+ });
371
+
372
+ // 2. Remote stylesheet (Google Fonts, etc.). Emits `@import url(...)` and still
373
+ // registers the CSS variable so usage stays the same as the @font-face flow.
374
+ export const InterCdn = defineFont({
375
+ name: 'Inter',
376
+ variable: '--font-inter',
377
+ import: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
378
+ });
379
+ ```
380
+
381
+ Example usage:
382
+
383
+ ```tsx
384
+ import { Inter } from './fonts.css';
385
+ import { styled } from '@salty-css/react/styled';
386
+
387
+ // Apply the font globally by attaching its className high up in the tree.
388
+ // This sets `--font-inter` on the subtree and applies `font-family: var(--font-inter)`.
389
+ export const App = ({ children }) => <div className={Inter.className}>{children}</div>;
390
+
391
+ // `Inter` stringifies to its font-family value (with fallbacks), so it can be used directly.
392
+ export const Heading = styled('h1', {
393
+ base: {
394
+ fontFamily: `${Inter}`,
395
+ },
396
+ });
397
+
398
+ // Or reference the CSS variable explicitly.
399
+ export const Body = styled('p', {
400
+ base: {
401
+ fontFamily: `var(${Inter.variable})`,
402
+ },
403
+ });
404
+ ```
405
+
406
+ ## Importing additional CSS
407
+
408
+ Use `defineImport` to pull in CSS that lives outside of Salty's authoring API — a reset stylesheet from npm, a Google Fonts URL, an asset in your app's `public/` folder, or a sibling `.css` file. The compiler turns each spec into an `@import` rule in the generated `saltygen/index.css`, so the imported stylesheets travel with the rest of your build.
409
+
410
+ ```ts
411
+ // /styles/imports.css.ts
412
+ import { defineImport } from '@salty-css/core/factories';
413
+
414
+ export default defineImport(
415
+ // Relative to this file
416
+ './reset.css',
417
+ // From node_modules (bare specifier — same as Vite / native CSS @import)
418
+ 'modern-normalize/modern-normalize.css',
419
+ // From node_modules (~ prefix — same resolver, webpack-style)
420
+ '~normalize.css/normalize.css',
421
+ // From your app's public/ folder (served at the host root)
422
+ '/fonts/inter.css',
423
+ // External URL
424
+ 'https://fonts.googleapis.com/css2?family=Inter&display=swap',
425
+ // Object form — attach media or supports() conditions
426
+ { url: './print.css', media: 'print' },
427
+ { url: './p3.css', supports: 'color(display-p3 1 1 1)' }
428
+ );
429
+ ```
430
+
431
+ Path resolution:
432
+
433
+ | Pattern | Behaviour |
434
+ | --------------------------- | ---------------------------------------------------------------------------------------- |
435
+ | `http://`, `https://`, `//` | Emitted verbatim |
436
+ | Starts with `/` | Public-folder URL — emitted verbatim, the browser resolves it against your host |
437
+ | Starts with `./` or `../` | Resolved at build time relative to the file that called `defineImport` |
438
+ | `~package/file.css` | Stripped of the leading `~`, then resolved from `node_modules` and copied into the build |
439
+ | `package/file.css` (bare) | Same `node_modules` resolution as the `~` form |
440
+
441
+ All imports are placed inside a new `imports` cascade layer that sits **before** `reset`, `global`, `templates`, and your component styles. This means your own styles always win over third-party CSS you pull in — which is what most teams expect when they drop in something like `modern-normalize`.
442
+
443
+ The full layer order in the generated `index.css` is:
444
+
445
+ ```css
446
+ @layer imports, reset, global, templates, l0, l1, l2, l3, l4, l5, l6, l7, l8;
447
+ ```
448
+
322
449
  ## Keyframes animations
323
450
 
324
451
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salty-css/react",
3
- "version": "0.1.0-alpha.25",
3
+ "version": "0.1.0-alpha.27",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "typings": "./dist/index.d.ts",
@@ -78,7 +78,7 @@
78
78
  }
79
79
  },
80
80
  "dependencies": {
81
- "@salty-css/core": "^0.1.0-alpha.25",
81
+ "@salty-css/core": "^0.1.0-alpha.27",
82
82
  "clsx": ">=2.x",
83
83
  "react": ">=19.x || >=18.3.x"
84
84
  },