Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): Add types for the object form of style #8464

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-trees-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add types for the object syntax for `style` (ex: `style={{color: 'red'}}`)
29 changes: 28 additions & 1 deletion packages/astro/astro-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,33 @@ declare namespace astroHTML.JSX {
| 'treegrid'
| 'treeitem';

type CssProperty = keyof Omit<
CSSStyleDeclaration,
| 'item'
| 'setProperty'
| 'removeProperty'
| 'getPropertyValue'
| 'getPropertyPriority'
| 'parentRule'
| 'length'
| 'cssFloat'
| 'cssText'
| typeof Symbol.iterator
| number
>;

type KebabCSSDOMProperties = import('./dist/type-utils.js').KebabKeys<DOMCSSProperties>;

type DOMCSSProperties = {
[key in CssProperty]?: string | number | null | undefined;
};
type AllCSSProperties = {
[key: string]: string | number | null | undefined;
};
type StyleObject = import('./dist/type-utils.js').Simplify<
KebabCSSDOMProperties & DOMCSSProperties & AllCSSProperties
>;

interface HTMLAttributes extends AriaAttributes, DOMAttributes, AstroBuiltinAttributes {
// Standard HTML Attributes
accesskey?: string | undefined | null;
Expand Down Expand Up @@ -513,7 +540,7 @@ declare namespace astroHTML.JSX {
lang?: string | undefined | null;
slot?: string | undefined | null;
spellcheck?: 'true' | 'false' | boolean | undefined | null;
style?: string | Record<string, any> | undefined | null;
style?: string | StyleObject | undefined | null;
tabindex?: number | string | undefined | null;
title?: string | undefined | null;
translate?: 'yes' | 'no' | undefined | null;
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ export type OmitIndexSignature<ObjectType> = {
: KeyType]: ObjectType[KeyType];
};

// Transform a string into its kebab case equivalent (camelCase -> kebab-case). Useful for CSS-in-JS to CSS.
export type Kebab<T extends string, A extends string = ''> = T extends `${infer F}${infer R}`
? Kebab<R, `${A}${F extends Lowercase<F> ? '' : '-'}${Lowercase<F>}`>
: A;

// Transform every key of an object to its kebab case equivalent using the above utility
export type KebabKeys<T> = { [K in keyof T as K extends string ? Kebab<K> : K]: T[K] };

// Similar to `keyof`, gets the type of all the values of an object
export type ValueOf<T> = T[keyof T];
5 changes: 5 additions & 0 deletions packages/astro/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export type HTMLAttributes<Tag extends HTMLTag> = Omit<
keyof Omit<AstroBuiltinAttributes, 'class:list'>
>;

/**
* All the CSS properties available, as defined by the CSS specification
*/
export type CSSProperty = keyof astroHTML.JSX.KebabCSSDOMProperties;

type PolymorphicAttributes<P extends { as: HTMLTag }> = Omit<P & HTMLAttributes<P['as']>, 'as'> & {
as?: P['as'];
};
Expand Down
Loading