Skip to content

Commit 2c4461a

Browse files
ralucaStanclaude
andauthored
Style Engine: Export public TypeScript types (#79079)
* Style engine: export public TypeScript types Re-export the `Style`, `StyleOptions`, and `GeneratedCSSRule` types from the package entry point so consumers of `@wordpress/style-engine` can type their usage of `compileCSS` and `getCSSRules`. These are the types that appear in the public function signatures; internal helper types remain unexported, matching the convention used by `rich-text` and `components`. Each exported type is documented at the export site so the autogenerated API docs render a description, and the README is regenerated accordingly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Style engine: differentiate type doc comments per review feedback Use a concise summary at each export (which docgen renders into the README) and a fuller description at each type definition (which IDE tooling surfaces on hover), so the two are no longer identical. Matches the pattern used by the `rich-text` and `worker-threads` packages. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Style engine: include backgrounds in the Style type description The `Style` interface gained a top-level `background` key; reword its definition comment to mention backgrounds and phrase the property list as non-exhaustive ("such as") so it stays accurate as the type evolves. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Style engine: add @example usage to compileCSS and getCSSRules Document both functions with an @example JSDoc block so docgen renders a Usage example into the README's autogenerated API section. This makes the exported Style, StyleOptions and GeneratedCSSRule types learnable in context (selector vs. inline output, structured rules), with the example colocated with the code as a single source of truth rather than hand-written README prose. Outputs were verified against the package's behaviour. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 552f8d9 commit 2c4461a

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

packages/style-engine/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- Export the `Style`, `StyleOptions`, and `GeneratedCSSRule` TypeScript types so that consumers of the public API can type their usage of `compileCSS` and `getCSSRules`.
8+
59
## 2.48.0 (2026-06-10)
610

711
### Code Quality

packages/style-engine/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ _This package assumes that your code will run in an **ES2015+** environment. If
228228

229229
Generates a stylesheet for a given style object and selector.
230230

231+
_Usage_
232+
233+
```js
234+
import { compileCSS } from '@wordpress/style-engine';
235+
236+
// With a selector, a full stylesheet string is returned.
237+
compileCSS(
238+
{ spacing: { padding: '10px', margin: '12px' } },
239+
{ selector: '.my-block' }
240+
);
241+
// '.my-block { margin: 12px; padding: 10px; }'
242+
243+
// Without a selector, inline declarations are returned.
244+
compileCSS( { spacing: { padding: '10px', margin: '12px' } } );
245+
// 'margin: 12px; padding: 10px;'
246+
```
247+
231248
_Parameters_
232249

233250
- _style_ `Style`: Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
@@ -241,10 +258,29 @@ _Changelog_
241258

242259
`6.1.0` Introduced in WordPress core.
243260

261+
### GeneratedCSSRule
262+
263+
A single CSS rule produced by `getCSSRules`.
264+
244265
### getCSSRules
245266

246267
Returns a JSON representation of the generated CSS rules.
247268

269+
_Usage_
270+
271+
```js
272+
import { getCSSRules } from '@wordpress/style-engine';
273+
274+
getCSSRules(
275+
{ spacing: { padding: '10px', margin: '12px' } },
276+
{ selector: '.my-block' }
277+
);
278+
// [
279+
// { selector: '.my-block', key: 'margin', value: '12px' },
280+
// { selector: '.my-block', key: 'padding', value: '10px' },
281+
// ]
282+
```
283+
248284
_Parameters_
249285

250286
- _style_ `Style`: Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
@@ -276,6 +312,14 @@ _Returns_
276312

277313
- `StyleValue`: A CSS custom var value if the incoming style value is a preset value.
278314

315+
### Style
316+
317+
A block or theme style object accepted by `compileCSS` and `getCSSRules`.
318+
319+
### StyleOptions
320+
321+
Options for `compileCSS` and `getCSSRules`.
322+
279323
<!-- END TOKEN(Autogenerated API docs) -->
280324

281325
## Glossary

packages/style-engine/src/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ import type {
1414
} from './types';
1515
import { styleDefinitions } from './styles';
1616

17+
/**
18+
* A block or theme style object accepted by `compileCSS` and `getCSSRules`.
19+
*/
20+
export type { Style } from './types';
21+
22+
/**
23+
* Options for `compileCSS` and `getCSSRules`.
24+
*/
25+
export type { StyleOptions } from './types';
26+
27+
/**
28+
* A single CSS rule produced by `getCSSRules`.
29+
*/
30+
export type { GeneratedCSSRule } from './types';
31+
1732
/**
1833
* Generates a stylesheet for a given style object and selector.
1934
*
@@ -22,6 +37,22 @@ import { styleDefinitions } from './styles';
2237
* @param style Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
2338
* @param options Options object with settings to adjust how the styles are generated.
2439
*
40+
* @example
41+
* ```js
42+
* import { compileCSS } from '@wordpress/style-engine';
43+
*
44+
* // With a selector, a full stylesheet string is returned.
45+
* compileCSS(
46+
* { spacing: { padding: '10px', margin: '12px' } },
47+
* { selector: '.my-block' }
48+
* );
49+
* // '.my-block { margin: 12px; padding: 10px; }'
50+
*
51+
* // Without a selector, inline declarations are returned.
52+
* compileCSS( { spacing: { padding: '10px', margin: '12px' } } );
53+
* // 'margin: 12px; padding: 10px;'
54+
* ```
55+
*
2556
* @return A generated stylesheet or inline style declarations.
2657
*/
2758
export function compileCSS( style: Style, options: StyleOptions = {} ): string {
@@ -76,6 +107,20 @@ export function compileCSS( style: Style, options: StyleOptions = {} ): string {
76107
* @param style Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
77108
* @param options Options object with settings to adjust how the styles are generated.
78109
*
110+
* @example
111+
* ```js
112+
* import { getCSSRules } from '@wordpress/style-engine';
113+
*
114+
* getCSSRules(
115+
* { spacing: { padding: '10px', margin: '12px' } },
116+
* { selector: '.my-block' }
117+
* );
118+
* // [
119+
* // { selector: '.my-block', key: 'margin', value: '12px' },
120+
* // { selector: '.my-block', key: 'padding', value: '10px' },
121+
* // ]
122+
* ```
123+
*
79124
* @return A collection of objects containing the selector, if any, the CSS property key (camelcase) and parsed CSS value.
80125
*/
81126
export function getCSSRules(

packages/style-engine/src/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export interface BorderIndividualStyles< T extends BoxEdge > {
2020
width?: CSSProperties[ `border${ Capitalize< T > }Width` ];
2121
}
2222

23+
/**
24+
* A style object — for example a block's `attributes.style` or the top-level
25+
* styles in `theme.json`. Groups visual style properties such as color,
26+
* typography, spacing, dimensions, borders, backgrounds and element styles,
27+
* and is the input `compileCSS` and `getCSSRules` turn into CSS.
28+
*/
2329
export interface Style {
2430
background?: {
2531
backgroundImage?:
@@ -89,13 +95,22 @@ export interface CssRulesKeys {
8995
individual: string;
9096
}
9197

98+
/**
99+
* Options that adjust how styles are generated — notably the CSS `selector` to
100+
* scope the output to. With no selector, declarations are returned inline.
101+
*/
92102
export interface StyleOptions {
93103
/**
94104
* CSS selector for the generated style.
95105
*/
96106
selector?: string;
97107
}
98108

109+
/**
110+
* A single generated CSS rule: an optional `selector`, the `value`, and the
111+
* `key` in React/JS style-attribute format (e.g. `paddingTop` rather than
112+
* `padding-top`).
113+
*/
99114
export interface GeneratedCSSRule {
100115
selector?: string;
101116
value: string | unknown;

0 commit comments

Comments
 (0)