Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export type PropAndMeta = {
// - when they open props panel again, the prop is not there
//
// We want to avoid this if possible, but for some types like "asset" we can't
export const getStartingValue = (meta: PropMeta): PropValue | undefined => {
const getStartingValue = (
meta: PropMeta,
defaultBooleanValue: boolean
): PropValue | undefined => {
if (meta.type === "string" && meta.control !== "file") {
return {
type: "string",
Expand All @@ -61,7 +64,7 @@ export const getStartingValue = (meta: PropMeta): PropValue | undefined => {
if (meta.type === "boolean") {
return {
type: "boolean",
value: meta.defaultValue ?? false,
value: meta.defaultValue ?? defaultBooleanValue,
};
}

Expand All @@ -80,15 +83,6 @@ export const getStartingValue = (meta: PropMeta): PropValue | undefined => {
}
};

const getStartingProp = (
instanceId: Instance["id"],
meta: PropMeta,
name: string
): Prop | undefined => {
const value = getStartingValue(meta);
return value && { id: nanoid(), instanceId, name, ...value };
};

const getDefaultMetaForType = (type: Prop["type"]): PropMeta => {
switch (type) {
case "action":
Expand Down Expand Up @@ -258,15 +252,19 @@ export const usePropsLogic = ({

// For initial props, if prop is not saved, we want to show default value if available.
//
// Important to not use getStartingProp if default is not available
// Important to not use infer stating value if default is not available
// beacuse user may have this experience:
// - they open props panel of an Image
// - they see 0 in the control for "width"
// - where 0 is a fallback when no default is available
// - they think that width is set to 0, but it's actually not set at all
//
if (prop === undefined && propMeta.defaultValue !== undefined) {
prop = getStartingProp(instance.id, propMeta, name);
// initial properties are not defined but suggested to default so default boolean is false
const value = getStartingValue(propMeta, false);
if (value) {
prop = { id: nanoid(), instanceId: instance.id, name, ...value };
}
}

initialProps.push({
Expand Down Expand Up @@ -303,9 +301,14 @@ export const usePropsLogic = ({
// In case of custom property/attribute we get a string.
const propMeta =
propsMetas.get(propName) ?? getDefaultMetaForType("string");
const prop = getStartingProp(instance.id, propMeta, propName);
if (prop) {
updateProp(prop);
const value = getStartingValue(propMeta, true);
if (value) {
updateProp({
id: nanoid(),
instanceId: instance.id,
name: propName,
...value,
});
}
};

Expand Down
Loading