-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9eec1cc
commit a0dd9fd
Showing
12 changed files
with
282 additions
and
4 deletions.
There are no files selected for viewing
Submodule reflect-core
updated
from e3c052 to ab0498
132 changes: 132 additions & 0 deletions
132
packages/builder-web-core/widgets-native/html-input/html-input-checkbox.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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import type { ElementCssStyleData } from "@coli.codes/css"; | ||
import type { | ||
BorderSide, | ||
Color, | ||
ICheckboxManifest, | ||
IWHStyleWidget, | ||
MouseCursor, | ||
} from "@reflect-ui/core"; | ||
import { WidgetKey } from "../../widget-key"; | ||
import type { StylableJSXElementConfig } from "../../widget-core"; | ||
import { Container } from "../container"; | ||
import * as css from "@web-builder/styles"; | ||
import { JSX, JSXAttribute, StringLiteral } from "coli"; | ||
|
||
/** | ||
* A jsx attibute to indicate input type as checkbox | ||
*/ | ||
const attr_type_range = new JSXAttribute("type", new StringLiteral("checkbox")); | ||
|
||
/** | ||
* checkbox | ||
* | ||
* | ||
* @see | ||
* - [html#progress](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress) | ||
*/ | ||
export class HtmlInputCheckbox extends Container implements ICheckboxManifest { | ||
_type = "input/checkbox"; | ||
|
||
value?: boolean; | ||
|
||
/** | ||
* @deprecated - not supported natively | ||
*/ | ||
tristate?: boolean; | ||
mouseCursor?: MouseCursor; | ||
activeColor?: Color; | ||
// fillColor?: ReflectStateProperty<Color>; | ||
checkColor?: Color; | ||
focusColor?: Color; | ||
|
||
/** | ||
* @deprecated - not supported natively | ||
*/ | ||
hoverColor?: Color; | ||
// overlayColor?: ReflectStateProperty<Color>; | ||
|
||
/** | ||
* @deprecated - not supported natively | ||
*/ | ||
splashRadius?: number; | ||
// visualDensity?: VisualDensity, | ||
autofocus?: boolean; | ||
// shape?: OutlinedBorder; | ||
|
||
/** | ||
* @deprecated - not supported natively | ||
*/ | ||
side?: BorderSide; | ||
|
||
constructor({ | ||
key, | ||
|
||
value, | ||
tristate, | ||
mouseCursor, | ||
activeColor, | ||
checkColor, | ||
focusColor, | ||
hoverColor, | ||
splashRadius, | ||
autofocus, | ||
side, | ||
|
||
...rest | ||
}: { | ||
key: WidgetKey; | ||
} & IWHStyleWidget & | ||
ICheckboxManifest) { | ||
super({ key, ...rest }); | ||
|
||
this.value = value; | ||
this.tristate = tristate; | ||
this.mouseCursor = mouseCursor; | ||
this.activeColor = activeColor; | ||
this.checkColor = checkColor; | ||
this.focusColor = focusColor; | ||
this.hoverColor = hoverColor; | ||
this.splashRadius = splashRadius; | ||
this.autofocus = autofocus; | ||
this.side = side; | ||
} | ||
|
||
get indeterminate() { | ||
return this.value === undefined; | ||
} | ||
|
||
styleData(): ElementCssStyleData { | ||
const containerstyle = super.styleData(); | ||
|
||
return { | ||
// general layouts, continer --------------------- | ||
...containerstyle, | ||
// ------------------------------------------------- | ||
|
||
"border-radius": undefined, // clear to use default appearance | ||
"background-color": undefined, // clear to use default appearance | ||
padding: undefined, | ||
|
||
// general slider styles | ||
"accent-color": css.color(this.activeColor), | ||
}; | ||
} | ||
|
||
jsxConfig(): StylableJSXElementConfig { | ||
const attrs = [ | ||
attr_type_range, | ||
this.indeterminate | ||
? new JSXAttribute("indeterminate") | ||
: new JSXAttribute( | ||
"value", | ||
new StringLiteral(this.value ? "checked" : "unchecked") | ||
), | ||
].filter(Boolean); | ||
|
||
return <StylableJSXElementConfig>{ | ||
type: "tag-and-attr", | ||
tag: JSX.identifier("input"), | ||
attributes: attrs, | ||
}; | ||
} | ||
} |
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,2 +1,3 @@ | ||
export { HtmlTextField as TextInput } from "./html-input-text"; | ||
export { HtmlSlider as Slider } from "./html-input-range"; | ||
export { HtmlInputCheckbox as Checkbox } from "./html-input-checkbox"; |
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
86 changes: 86 additions & 0 deletions
86
packages/designto-token/support-flags/token-checkbox/index.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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Checkbox, Container, ICheckboxManifest } from "@reflect-ui/core"; | ||
import type { AsCheckboxFlag } from "@code-features/flags"; | ||
import type { | ||
ReflectFrameNode, | ||
ReflectSceneNode, | ||
} from "@design-sdk/figma-node"; | ||
import { WrappingContainer } from "../../tokens"; | ||
import assert from "assert"; | ||
import { unwrappedChild } from "../../wrappings"; | ||
import { tokenizeLayout } from "../../token-layout"; | ||
import { keyFromNode } from "../../key"; | ||
|
||
export function tokenize_flagged_checkbox( | ||
node: ReflectSceneNode, | ||
flag: AsCheckboxFlag | ||
): Checkbox | WrappingContainer<Checkbox> { | ||
if (flag.value === false) return; | ||
|
||
const validated = validate_checkbox(node); | ||
if (validated.error === false) { | ||
switch (validated.__type) { | ||
case "frame-as-checkbox": { | ||
const { checkbox_base, checkbox_value } = validated; | ||
|
||
const _key = keyFromNode(node); | ||
|
||
const container = unwrappedChild( | ||
tokenizeLayout.fromFrame( | ||
checkbox_base, | ||
checkbox_base.children, | ||
{ is_root: false }, | ||
{} | ||
) | ||
) as Container; | ||
|
||
const checked = checkbox_value && checkbox_value.visible; | ||
|
||
return new WrappingContainer({ | ||
...container, | ||
key: keyFromNode(node), | ||
child: new Checkbox({ | ||
key: _key, | ||
fillColor: { default: checkbox_base?.primaryColor }, | ||
checkColor: checkbox_value?.primaryColor, | ||
value: checked, | ||
}), | ||
}); | ||
} | ||
default: | ||
throw new Error("unreachable"); | ||
} | ||
} else { | ||
throw new Error(validated.error); | ||
} | ||
} | ||
|
||
/** | ||
* validate if layer casted as checkbox can be actually tokenized to checkbox. | ||
* @param node | ||
*/ | ||
function validate_checkbox(node: ReflectSceneNode): | ||
| { | ||
__type: "frame-as-checkbox"; | ||
error: false; | ||
checkbox_base: ReflectFrameNode; | ||
checkbox_value?: ReflectSceneNode; | ||
} | ||
| { error: string } { | ||
assert(!!node, "target must not be null or undefined"); | ||
switch (node.type) { | ||
case "FRAME": { | ||
const firstvaluenode = node.children.find( | ||
(child) => child.type === "VECTOR" | ||
); | ||
|
||
return { | ||
__type: "frame-as-checkbox", | ||
checkbox_base: node, | ||
checkbox_value: firstvaluenode, | ||
error: false, | ||
}; | ||
} | ||
default: | ||
return { error: "checkbox target is not a valid frame node" }; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/designto-web/tokens-to-web-widget/compose-unwrapped-checkbox.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as web from "@web-builder/core"; | ||
import * as core from "@reflect-ui/core"; | ||
|
||
export function compose_unwrapped_checkbox( | ||
key, | ||
widget: core.Checkbox, | ||
container?: core.Container | ||
): web.Checkbox { | ||
return new web.Checkbox({ ...(container ?? {}), ...widget, key }); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// primary | ||
export const flag_key__as_checkbox = "as-checkbox"; | ||
|
||
export const flag_key_alias__as_checkbox = [flag_key__as_checkbox]; | ||
|
||
export interface AsCheckboxFlag { | ||
flag: typeof flag_key__as_checkbox; | ||
|
||
value: boolean; | ||
_raw?: string; | ||
} |
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.
a0dd9fd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: