-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new CSSDirective design (#5835)
* feat: new CSSDirective design * feat: attach the partial helper to the css helper * fix: update foundation to new CSSDirective API * fix: update components to new CSSDirective API * docs: add missing docs to CSSTEmplateTag type * Change files * fix: add back cssPartial with deprecation message Co-authored-by: EisenbergEffect <[email protected]>
- Loading branch information
1 parent
1d759ed
commit de08455
Showing
11 changed files
with
211 additions
and
99 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-components-0f5602ef-a1c2-4f35-83a0-3e883906645e.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "fix: update components to new CSSDirective API", | ||
"packageName": "@microsoft/fast-components", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-element-e7b9adf7-1b40-4e7e-896f-c29600f26654.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "feat: new CSSDirective design", | ||
"packageName": "@microsoft/fast-element", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-foundation-b8a22db2-e6e3-4ba0-a9e1-725a4ef68eda.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "fix: update foundation to new CSSDirective API", | ||
"packageName": "@microsoft/fast-foundation", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { cssPartial } from "@microsoft/fast-element"; | ||
import { css } from "@microsoft/fast-element"; | ||
import { baseHeightMultiplier, density, designUnit } from "../design-tokens.js"; | ||
|
||
/** | ||
* A formula to retrieve the control height. | ||
* Use this as the value of any CSS property that | ||
* accepts a pixel size. | ||
*/ | ||
export const heightNumber = cssPartial`(${baseHeightMultiplier} + ${density}) * ${designUnit}`; | ||
export const heightNumber = css.partial`(${baseHeightMultiplier} + ${density}) * ${designUnit}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 47 additions & 9 deletions
56
packages/web-components/fast-element/src/styles/css-directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,63 @@ | ||
import type { Constructable } from "../interfaces.js"; | ||
import type { Behavior } from "../observation/behavior.js"; | ||
import { createTypeRegistry } from "../platform.js"; | ||
import type { ComposableStyles } from "./element-styles.js"; | ||
|
||
/** | ||
* Used to add behaviors when constructing styles. | ||
* @public | ||
*/ | ||
export type AddBehavior = (behavior: Behavior<HTMLElement>) => void; | ||
|
||
/** | ||
* Directive for use in {@link css}. | ||
* | ||
* @public | ||
*/ | ||
export class CSSDirective { | ||
export interface CSSDirective { | ||
/** | ||
* Creates a CSS fragment to interpolate into the CSS document. | ||
* @returns - the string to interpolate into CSS | ||
*/ | ||
public createCSS(): ComposableStyles { | ||
return ""; | ||
} | ||
createCSS(add: AddBehavior): ComposableStyles; | ||
} | ||
|
||
/** | ||
* Defines metadata for a CSSDirective. | ||
* @public | ||
*/ | ||
export interface CSSDirectiveDefinition< | ||
TType extends Constructable<CSSDirective> = Constructable<CSSDirective> | ||
> { | ||
/** | ||
* Creates a behavior to bind to the host element. | ||
* @returns - the behavior to bind to the host element, or undefined. | ||
* The type that the definition provides metadata for. | ||
*/ | ||
public createBehavior(): Behavior<HTMLElement> | undefined { | ||
return undefined; | ||
} | ||
readonly type: TType; | ||
} | ||
|
||
const registry = createTypeRegistry<CSSDirectiveDefinition>(); | ||
|
||
/** | ||
* Instructs the css engine to provide dynamic styles or | ||
* associate behaviors with styles. | ||
* @public | ||
*/ | ||
export const CSSDirective = Object.freeze({ | ||
getForInstance: registry.getForInstance, | ||
getByType: registry.getByType, | ||
define<TType extends Constructable<CSSDirective>>(type): TType { | ||
registry.register({ type }); | ||
return type; | ||
}, | ||
}); | ||
|
||
/** | ||
* Decorator: Defines a CSSDirective. | ||
* @public | ||
*/ | ||
export function cssDirective() { | ||
/* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */ | ||
return function (type: Constructable<CSSDirective>) { | ||
CSSDirective.define(type); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.