-
Some help in this regard might be needed. There is this example that shows a const dom3 = div(
{style: {deps: [counter], f: c => `font-size: ${c}em;`}},
"Text")
I am still wrapping my head with the reactivity model to try to get-it, but it is so fun! An example export interface ButtonProps<E extends HTMLButtonElement | HTMLAnchorElement | HTMLElement = HTMLButtonElement> extends ActionProps<E> {
children?: ChildDom[];
icon?: IconName | Node | [];
rightIcon?: IconName | Node | [];
alignText?: Alignment;
intent?: Intent;
active?: boolean | State<boolean>;
fill?: boolean | State<boolean>;
large?: boolean | State<boolean>;
loading?: boolean | State<boolean>;
minimal?: boolean | State<boolean>;
outlined?: boolean | State<boolean>;
small?: boolean | State<boolean>;
type?: "submit" | "reset" | "button";
}
const { button, span } = van.tags;
export const Button = (props: ButtonProps = {}, ...rest: readonly ChildDom[]) => {
const componentClasses = classNames(
// default class
Classes.BUTTON,
// reactive
props.active ? Classes.ACTIVE : null,
props.minimal ? Classes.MINIMAL : null,
props.outlined ? Classes.OUTLINED : null,
props.fill ? Classes.FILL : null,
props.small ? Classes.SMALL : null,
props.alignText ? AlignmentClassMap[props.alignText] : null,
props.intent ? IntentClassMap[props.intent] : null
);
const createText = () => {
return props.text ? span({ class: Classes.BUTTON_TEXT }, props.text) : [];
};
const createIcon = (icon?: IconName | Node | []) => {
return icon ? Icon({ icon }) : [];
};
return button(
// attributes
{
class: componentClasses,
onclick: (e: MouseEvent) => {
if (props?.onClick) {
props.onClick(e);
}
},
},
// children
createIcon(props?.icon),
...(props?.children || []).concat(rest),
createText(),
createIcon(props.rightIcon)
);
};
Button.displayName = `${DISPLAYNAME_PREFIX}.Button`; How can one make reactive I've tried this hereunder but because props are either const componentClasses = {
deps: [props.active, props.minimal, props.outlined, props.fill, props.small, props.alignText, props.intent],
//
(active, minimal, outlined, fill, small, alignText, intent) => classNames(
// default class
Classes.BUTTON,
// reactive
active ? Classes.ACTIVE : null,
minimal ? Classes.MINIMAL : null,
outlined ? Classes.OUTLINED : null,
fill ? Classes.FILL : null,
small ? Classes.SMALL : null,
alignText ? AlignmentClassMap[props.alignText] : null,
intent ? IntentClassMap[props.intent] : null
)
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
Custom attributes of native DOM elements are supported in the same way as properties. When the field of
For custom components (which is just a plain JavaScript function), there is no builtin support for state-derived properties, although you can provide similar support in the implementation of the component function. I have a feeling that for most use cases of custom components, state-derived properties might not be that necessary. Unlike native DOM elements, where some properties need to be provided in a specific way as defined in HTML specification (for instance, That said, if you feel that you have a strong use case where state-derived properties are necessary for custom components, you can let me know. I will be curious to know if there is a straightforward solution to your problem. Meanwhile, the real-world use cases provide great feedback for the evolution of VanJS |
Beta Was this translation helpful? Give feedback.
Custom attributes of native DOM elements are supported in the same way as properties. When the field of
props
arg doesn't exist as an element property, VanJS will set theprop
value as a DOM attribute. This behavior is discussed at https://vanjs.org/advanced#dom-attributes-vs-propertiesFor custom components (which is just a plain JavaScript function), there is no builtin support for state-derived properties, although you can provide similar support in the imp…