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

This commit is contained in:
Erika 2023-09-13 17:57:59 +02:00 committed by GitHub
parent d93987824d
commit c92e0acd71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Add types for the object syntax for `style` (ex: `style={{color: 'red'}}`)

View file

@ -471,6 +471,33 @@ declare namespace astroHTML.JSX {
| 'treegrid' | 'treegrid'
| 'treeitem'; | '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 { interface HTMLAttributes extends AriaAttributes, DOMAttributes, AstroBuiltinAttributes {
// Standard HTML Attributes // Standard HTML Attributes
accesskey?: string | undefined | null; accesskey?: string | undefined | null;
@ -513,7 +540,7 @@ declare namespace astroHTML.JSX {
lang?: string | undefined | null; lang?: string | undefined | null;
slot?: string | undefined | null; slot?: string | undefined | null;
spellcheck?: 'true' | 'false' | boolean | 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; tabindex?: number | string | undefined | null;
title?: string | undefined | null; title?: string | undefined | null;
translate?: 'yes' | 'no' | undefined | null; translate?: 'yes' | 'no' | undefined | null;

View file

@ -17,5 +17,13 @@ export type OmitIndexSignature<ObjectType> = {
: KeyType]: ObjectType[KeyType]; : 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 // Similar to `keyof`, gets the type of all the values of an object
export type ValueOf<T> = T[keyof T]; export type ValueOf<T> = T[keyof T];

View file

@ -9,6 +9,11 @@ export type HTMLAttributes<Tag extends HTMLTag> = Omit<
keyof Omit<AstroBuiltinAttributes, 'class:list'> 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'> & { type PolymorphicAttributes<P extends { as: HTMLTag }> = Omit<P & HTMLAttributes<P['as']>, 'as'> & {
as?: P['as']; as?: P['as'];
}; };