Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Ability to link by #ID; Fixed a bug with links in RTE (#1454)

Roman Pominov8d9445962570
diff --git a/apps/builder/app/builder/features/props-panel/controls/combined.tsx b/apps/builder/app/builder/features/props-panel/controls/combined.tsx
index 0134e6091e..d1ef3146e3 100644
--- a/apps/builder/app/builder/features/props-panel/controls/combined.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/combined.tsx
@@ -13,7 +13,7 @@ export const renderControl = ({
meta,
prop,
...rest
-}: ControlProps<string, string>) => {
+}: ControlProps<string, string> & { key?: string }) => {
if (
meta.control === "text" &&
(prop === undefined || prop.type === "string")
diff --git a/apps/builder/app/builder/features/props-panel/controls/url.tsx b/apps/builder/app/builder/features/props-panel/controls/url.tsx
index 2358f8fa6a..ecf9625ab6 100644
--- a/apps/builder/app/builder/features/props-panel/controls/url.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/url.tsx
@@ -1,10 +1,11 @@
+import { type ReactNode, useState } from "react";
import { useStore } from "@nanostores/react";
-import { pagesStore } from "~/shared/nano-states";
+import { computed } from "nanostores";
import {
- InputField,
- Flex,
theme,
useId,
+ InputField,
+ Flex,
ToggleGroup,
ToggleGroupItem,
Select,
@@ -18,20 +19,25 @@ import {
PageIcon,
PhoneIcon,
} from "@webstudio-is/icons";
-import { type ReactNode, useState } from "react";
+import {
+ findTreeInstanceIds,
+ type Instance,
+ type Page,
+} from "@webstudio-is/project-build";
+import { instancesStore, pagesStore, propsStore } from "~/shared/nano-states";
import {
type ControlProps,
getLabel,
useLocalValue,
VerticalLayout,
} from "../shared";
-import type { Page } from "@webstudio-is/project-build";
import { SelectAsset } from "./select-asset";
type UrlControlProps = ControlProps<"url", "string" | "page" | "asset">;
type BaseControlProps = {
id: string;
+ instanceId: string;
prop: UrlControlProps["prop"];
onChange: UrlControlProps["onChange"];
onSoftDelete: UrlControlProps["onSoftDelete"];
@@ -175,29 +181,119 @@ const BaseEmail = ({ prop, onChange, id }: BaseControlProps) => {
);
};
-const getPageId = (page: Page) => page.id;
-const getPageName = (page: Page) => page.name;
+const instancesPerPageStore = computed(
+ [instancesStore, pagesStore],
+ (instances, pages) =>
+ (pages ? [pages.homePage, ...pages.pages] : []).map((page) => ({
+ pageId: page.id,
+ instancesIds: findTreeInstanceIds(instances, page.rootInstanceId),
+ }))
+);
-const BasePage = ({ prop, onChange, id }: BaseControlProps) => {
+const sectionsStore = computed(
+ [instancesPerPageStore, propsStore],
+ (instancesPerPage, props) => {
+ const sections: Array<{
+ pageId: Page["id"];
+ instanceId: Instance["id"];
+ hash: string;
+ }> = [];
+
+ for (const prop of props.values()) {
+ if (
+ prop.type === "string" &&
+ prop.name === "id" &&
+ prop.value.trim() !== ""
+ ) {
+ for (const { pageId, instancesIds } of instancesPerPage) {
+ if (instancesIds.has(prop.instanceId)) {
+ sections.push({
+ pageId,
+ instanceId: prop.instanceId,
+ hash: prop.value,
+ });
+ }
+ }
+ }
+ }
+
+ return sections.sort((a, b) => a.hash.localeCompare(b.hash));
+ }
+);
+
+const getId = (data: { id: string }) => data.id;
+const getName = (data: { name: string }) => data.name;
+const getHash = (data: { hash: string }) => data.hash;
+const getInstanceId = (data: { instanceId: string }) => data.instanceId;
+
+const BasePage = ({ prop, onChange }: BaseControlProps) => {
const pages = useStore(pagesStore);
- const options = pages === undefined ? [] : [pages.homePage, ...pages.pages];
- const value =
+
+ const pageSelectOptions =
+ pages === undefined ? [] : [pages.homePage, ...pages.pages];
+
+ const pageSelectValue =
prop?.type === "page"
- ? options.find((page) => page.id === prop.value)
+ ? pageSelectOptions.find(
+ (page) =>
+ page.id ===
+ (typeof prop.value === "string" ? prop.value : prop.value.pageId)
+ )
: undefined;
+ const sections = useStore(sectionsStore);
+
+ const sectionSelectOptions = pageSelectValue
+ ? sections.filter(({ pageId }) => pageId === pageSelectValue.id)
+ : sections;
+
+ const sectionInstanceId =
+ prop?.type === "page" && typeof prop.value !== "string"
+ ? prop.value.instanceId
+ : undefined;
+
+ const sectionSelectValue =
+ sectionInstanceId === undefined
+ ? undefined
+ : sectionSelectOptions.find(
+ ({ instanceId }) => instanceId === sectionInstanceId
+ );
+
return (
- <Row>
- <Select
- id={id}
- value={value}
- options={options}
- getLabel={getPageName}
- getValue={getPageId}
- onChange={(page) => onChange({ type: "page", value: page.id })}
- fullWidth
- />
- </Row>
+ <>
+ <Row>
+ <Select
+ value={pageSelectValue}
+ options={pageSelectOptions}
+ getLabel={getName}
+ getValue={getId}
+ onChange={({ id }) => onChange({ type: "page", value: id })}
+ placeholder="Choose page"
+ fullWidth
+ />
+ </Row>
+ <Row>
+ <Select
+ key={pageSelectValue?.id}
+ disabled={sectionSelectOptions.length === 0}
+ placeholder={
+ sectionSelectOptions.length === 0
+ ? pageSelectValue
+ ? "Selected page has no sections"
+ : "No sections available"
+ : "Choose section"
+ }
+ value={sectionSelectValue}
+ options={sectionSelectOptions}
+ getLabel={getHash}
+ getValue={getInstanceId}
+ onChange={({ pageId, instanceId }) =>
+ onChange({ type: "page", value: { pageId, instanceId } })
+ }
+ fullWidth
+ />
+ </Row>
+ </>
);
};
@@ -250,6 +346,7 @@ const propToMode = (prop?: UrlControlProps["prop"]): Mode => {
};
export const UrlControl = ({
+ instanceId,
meta,
prop,
propName,
@@ -297,6 +394,7 @@ export const UrlControl = ({
<BaseControl
id={id}
+ instanceId={instanceId}
prop={prop}
onChange={onChange}
onSoftDelete={onSoftDelete}
diff --git a/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx b/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx
index 00026d7b4e..7bd45b45ad 100644
--- a/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx
+++ b/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx
@@ -2,9 +2,15 @@ import { useState } from "react";
import { ButtonElementIcon } from "@webstudio-is/icons";
import { PropsPanel } from "./props-panel";
import { usePropsLogic } from "./use-props-logic";
-import { assetsStore, pagesStore } from "~/shared/nano-states";
+import {
+ assetsStore,
+ instancesStore,
+ pagesStore,
+ propsStore,
+ selectedPageIdStore,
+} from "~/shared/nano-states";
import { setMockEnv } from "~/shared/env";
-import type { Prop } from "@webstudio-is/project-build";
+import type { Instance, Prop } from "@webstudio-is/project-build";
import type {
WsComponentMeta,
WsComponentPropsMeta,
@@ -40,6 +46,55 @@ pagesStore.set({
],
});
+const getSectionInstanceId = (
+ name: string,
+ page = pagesStore.get()?.homePage
+) => (page === undefined ? "" : `${page.id}-${name}`);
+
+const addLinkableSections = (
+ names: string[],
+ page = pagesStore.get()?.homePage
+) => {
+ if (page === undefined) {
+ return;
+ }
+
+ const instances = instancesStore.get();
+ const props = propsStore.get();
+
+ const rootInstance: Instance = {
+ id: page.rootInstanceId,
+ type: "instance",
+ component: "body",
+ children: [],
+ };
+ instances.set(rootInstance.id, rootInstance);
+
+ for (const name of names) {
+ const instance: Instance = {
+ id: getSectionInstanceId(name, page),
+ type: "instance",
+ component: "box",
+ children: [],
+ };
+ rootInstance.children.push({ type: "id", value: instance.id });
+ instances.set(instance.id, instance);
+
+ const prop: Prop = {
+ id: unique(),
+ instanceId: instance.id,
+ name: "id",
+ type: "string",
+ value: name,
+ };
+ props.set(prop.id, prop);
+ }
+};
+
+addLinkableSections(["contacts", "about"]);
+addLinkableSections(["company", "employees"], pagesStore.get()?.pages[0]);
+selectedPageIdStore.set(pagesStore.get()?.homePage.id);
+
const imageAsset = (name = "cat", format = "jpg"): Asset => ({
id: unique(),
projectId,
@@ -169,6 +224,7 @@ const componentPropsMeta: WsComponentPropsMeta = {
addedCheck: checkProp(),
addedUrlUrl: urlProp("Added URL (URL)"),
addedUrlPage: urlProp("Added URL (Page)"),
+ addedUrlSection: urlProp("Added URL (Section)"),
addedUrlEmail: urlProp("Added URL (Email)"),
addedUrlPhone: urlProp("Added URL (Phone)"),
addedUrlAttachment: urlProp("Added URL (Attachment)"),
@@ -269,6 +325,16 @@ const startingProps: Prop[] = [
type: "page",
value: pagesStore.get()?.pages[0].id ?? "",
},
+ {
+ id: unique(),
+ instanceId,
+ name: "addedUrlSection",
+ type: "page",
+ value: {
+ pageId: pagesStore.get()?.homePage.id ?? "",
+ instanceId: getSectionInstanceId("about"),
+ },
+ },
{
id: unique(),
instanceId,
@@ -327,6 +393,7 @@ export const Story = () => {
<div style={{ display: "flex", gap: 12 }}>
<div style={{ width: 240, border: "dashed 3px #e3e3e3" }}>
<PropsPanel
+ instanceId={instanceId}
propsLogic={logic}
component="Button"
instanceLabel="My Button"
diff --git a/apps/builder/app/builder/features/props-panel/props-panel.tsx b/apps/builder/app/builder/features/props-panel/props-panel.tsx
index 484d91e3c6..9396c768a3 100644
--- a/apps/builder/app/builder/features/props-panel/props-panel.tsx
+++ b/apps/builder/app/builder/features/props-panel/props-panel.tsx
@@ -1,5 +1,5 @@
import store from "immerhin";
-import type { Instance, Prop } from "@webstudio-is/project-build";
+import type { Instance } from "@webstudio-is/project-build";
import {
theme,
useCombobox,
@@ -8,13 +8,13 @@ import {
ComboboxAnchor,
ComboboxListbox,
ComboboxListboxItem,
- SmallIconButton,
Separator,
Flex,
Text,
type CSS,
ScrollArea,
InputField,
+ NestedSelectButton,
} from "@webstudio-is/design-system";
import { ChevronDownIcon } from "@webstudio-is/icons";
import type { Publish } from "~/shared/pubsub";
@@ -25,8 +25,12 @@ import {
type SetProperty as SetCssProperty,
} from "~/builder/features/style-panel/shared/use-style-data";
import { renderControl } from "./controls/combined";
-import { usePropsLogic, type NameAndLabel } from "./use-props-logic";
-import { type PropMeta, type PropValue, getLabel } from "./shared";
+import {
+ usePropsLogic,
+ type NameAndLabel,
+ type PropAndMeta,
+} from "./use-props-logic";
+import { getLabel } from "./shared";
import { useState, type ReactNode } from "react";
import {
getComponentPropsMeta,
@@ -93,9 +97,8 @@ const PropsCombobox = ({
{...combobox.getInputProps()}
placeholder="Property"
suffix={
- <SmallIconButton
+ <NestedSelectButton
{...combobox.getToggleButtonProps()}
- css={{ display: "flex", justifyContent: "center" }}
icon={<ChevronDownIcon />}
/>
}
@@ -120,37 +123,23 @@ const PropsCombobox = ({
);
};
-// @todo:
-// at this point we need the <Property> wrapper only because
-// of complicated `onChange` inside.
-// need to refactor this somehow
-const Property = ({
- meta,
- prop,
- propName,
- component,
- onChange,
- onDelete,
- onSoftDelete,
- setCssProperty,
-}: {
- prop: Prop | undefined;
- propName: string;
- meta: PropMeta;
- component: Instance["component"];
- onChange: (value: PropValue) => void;
- onDelete?: () => void;
- onSoftDelete: () => void;
- setCssProperty: SetCssProperty;
-}) =>
+const renderProperty = (
+ { propsLogic: logic, setCssProperty, component, instanceId }: PropsPanelProps,
+ { prop, propName, meta }: PropAndMeta,
+ deletable?: boolean
+) =>
renderControl({
+ key: propName,
+ instanceId,
meta,
prop,
propName,
- onDelete,
- onSoftDelete,
+ onDelete: deletable
+ ? () => logic.handleDelete({ prop, propName })
+ : undefined,
+ onSoftDelete: () => prop && logic.handleSoftDelete(prop),
onChange: (propValue, asset) => {
- onChange(propValue);
+ logic.handleChange({ prop, propName }, propValue);
// @todo: better way to do this?
if (
@@ -184,20 +173,19 @@ const AddPropertyForm = ({
</Flex>
);
-// A UI componet with minimum logic that can be demoed in Storybook etc.
-export const PropsPanel = ({
- propsLogic: logic,
- component,
- instanceLabel,
- componentMeta,
- setCssProperty,
-}: {
+type PropsPanelProps = {
propsLogic: ReturnType<typeof usePropsLogic>;
component: Instance["component"];
instanceLabel: string;
+ instanceId: string;
componentMeta: WsComponentMeta;
setCssProperty: SetCssProperty;
-}) => {
+};
+
+// A UI componet with minimum logic that can be demoed in Storybook etc.
+export const PropsPanel = (props: PropsPanelProps) => {
+ const { propsLogic: logic, instanceLabel, componentMeta } = props;
+
const [addingProp, setAddingProp] = useState(false);
const hasAddedProps = logic.addedProps.length > 0 || addingProp;
@@ -207,27 +195,29 @@ export const PropsPanel = ({
<Row>
<InstanceInfo meta={componentMeta} label={instanceLabel} />
</Row>
+
<Row
css={{
paddingTop: theme.spacing[3],
paddingBottom: theme.spacing[5],
}}
>
- {logic.initialProps.map(({ prop, propName, meta }) => (
- <Property
- key={propName}
- propName={propName}
- prop={prop}
- meta={meta}
- component={component}
- onChange={(value) => logic.handleChange({ prop, propName }, value)}
- onSoftDelete={() => prop && logic.handleSoftDelete(prop)}
- setCssProperty={setCssProperty}
- />
- ))}
+ {logic.systemProps.map((item) => renderProperty(props, item))}
</Row>
<Separator />
+
+ <Row
+ css={{
+ paddingTop: theme.spacing[5],
+ paddingBottom: theme.spacing[5],
+ }}
+ >
+ {logic.initialProps.map((item) => renderProperty(props, item))}
+ </Row>
+
+ <Separator />
+
<CollapsibleSectionWithAddButton
label="Properties"
onAdd={() => setAddingProp(true)}
@@ -235,22 +225,7 @@ export const PropsPanel = ({
>
{hasAddedProps && (
<Flex gap="2" direction="column">
- {logic.addedProps.map(({ prop, propName, meta }) => (
- <Property
- key={propName}
- propName={propName}
- prop={prop}
- meta={meta}
- component={component}
- onChange={(value) =>
- logic.handleChange({ prop, propName }, value)
- }
- onDelete={() => logic.handleDelete({ prop, propName })}
- onSoftDelete={() => prop && logic.handleSoftDelete(prop)}
- setCssProperty={setCssProperty}
- />
- ))}
-
+ {logic.addedProps.map((item) => renderProperty(props, item, true))}
{addingProp && (
<AddPropertyForm
availableProps={logic.remainingProps}
@@ -263,7 +238,6 @@ export const PropsPanel = ({
</Flex>
)}
</CollapsibleSectionWithAddButton>
- <Separator />
</ScrollArea>
);
};
@@ -312,6 +286,7 @@ export const PropsPanelContainer = ({
<PropsPanel
propsLogic={logic}
component={instance.component}
+ instanceId={instance.id}
instanceLabel={instanceLabel}
componentMeta={componentMeta}
setCssProperty={setCssProperty}
diff --git a/apps/builder/app/builder/features/props-panel/shared.tsx b/apps/builder/app/builder/features/props-panel/shared.tsx
index d2a859c9c6..a2acc9aeb5 100644
--- a/apps/builder/app/builder/features/props-panel/shared.tsx
+++ b/apps/builder/app/builder/features/props-panel/shared.tsx
@@ -40,6 +40,7 @@ type PropByType<Type> = Type extends string
: never;
export type ControlProps<Control, PropType> = {
+ instanceId: string;
meta: PropMetaByControl<Control>;
// prop is optional because we don't have it when an intial prop is not set
// and we don't want to show user something like a 0 for number when it's in fact not set to any value
diff --git a/apps/builder/app/builder/features/props-panel/use-props-logic.ts b/apps/builder/app/builder/features/props-panel/use-props-logic.ts
index 9617c8f846..4e461b21fd 100644
--- a/apps/builder/app/builder/features/props-panel/use-props-logic.ts
+++ b/apps/builder/app/builder/features/props-panel/use-props-logic.ts
@@ -86,24 +86,19 @@ type UsePropsLogicInput = {
};
type UsePropsLogicOutput = {
- /**
- * Initial (not deletable) props
- */
+ /** Similar to Initial, but displayed as a separate group in UI etc.
+ * Currentrly used only for the ID prop. */
+ systemProps: PropAndMeta[];
+ /** Initial (not deletable) props */
initialProps: PropAndMeta[];
- /**
- * Optional props that were added by user
- */
+ /** Optional props that were added by user */
addedProps: PropAndMeta[];
- /**
- * List of remaining props still available to add
- */
+ /** List of remaining props still available to add */
remainingProps: NameAndLabel[];
handleAdd: (propName: string) => void;
handleChange: (prop: PropOrName, value: PropValue) => void;
handleDelete: (prop: PropOrName) => void;
- /**
- * Delete the prop, but keep it in the list of added props
- */
+ /** Delete the prop, but keep it in the list of added props */
handleSoftDelete: (prop: Prop) => void;
};
@@ -113,9 +108,20 @@ const getAndDelete = <Value>(map: Map<string, Value>, key: string) => {
return value;
};
-/**
- * usePropsLogic expects that key={instanceId} is used on the ancestor component
- */
+const systemPropsMeta: { name: string; meta: PropMeta }[] = [
+ {
+ name: "id",
+ meta: {
+ required: false,
+ control: "text",
+ type: "string",
+ rows: 0,
+ label: "ID",
+ },
+ },
+];
+
+/** usePropsLogic expects that key={instanceId} is used on the ancestor component */
export const usePropsLogic = ({
props: savedProps,
meta,
@@ -127,7 +133,16 @@ export const usePropsLogic = ({
const unprocessedSaved = new Map(savedProps.map((prop) => [prop.name, prop]));
const unprocessedKnown = new Map(Object.entries(meta.props));
- const initialProps: PropAndMeta[] = (meta.initialProps ?? []).map((name) => {
+ const initialPropsNames = new Set(meta.initialProps ?? []);
+
+ const systemProps = systemPropsMeta.map(({ name, meta }) => {
+ const saved = getAndDelete(unprocessedSaved, name);
+ getAndDelete(unprocessedKnown, name);
+ initialPropsNames.delete(name);
+ return { prop: saved, propName: name, meta };
+ });
+
+ const initialProps: PropAndMeta[] = Array.from(initialPropsNames, (name) => {
const saved = getAndDelete(unprocessedSaved, name);
const known = getAndDelete(unprocessedKnown, name);
@@ -222,9 +237,11 @@ export const usePropsLogic = ({
};
return {
+ systemProps,
initialProps,
addedProps,
- remainingProps: Array.from(unprocessedKnown.entries()).map(
+ remainingProps: Array.from(
+ unprocessedKnown.entries(),
([name, { label }]) => ({ name, label })
),
handleAdd,
diff --git a/apps/builder/app/canvas/features/webstudio-component/link.ts b/apps/builder/app/canvas/features/webstudio-component/link.ts
index dfdfbd40f4..54a9004f2b 100644
--- a/apps/builder/app/canvas/features/webstudio-component/link.ts
+++ b/apps/builder/app/canvas/features/webstudio-component/link.ts
@@ -33,7 +33,9 @@ export const handleLinkClick = (event: MouseEvent) => {
return;
}
- const page = findPageByIdOrPath(pages, href);
+ const [withoutHash] = href.split("#");
+
+ const page = findPageByIdOrPath(pages, withoutHash);
if (page) {
publish({ type: "switchPage", payload: { pageId: page.id } });
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index 312b8d7bb4..cd84013500 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -118,7 +118,7 @@ export const WebstudioComponentDev = ({
return result;
}
for (const item of instanceProps) {
- if (item.type !== "asset") {
+ if (item.type !== "asset" && item.type !== "page") {
result[item.name] = item.value;
}
}
diff --git a/packages/design-system/src/components/select.tsx b/packages/design-system/src/components/select.tsx
index bc8cfd0c02..314decec54 100644
--- a/packages/design-system/src/components/select.tsx
+++ b/packages/design-system/src/components/select.tsx
@@ -27,7 +27,11 @@ export const SelectSeparator = styled(Primitive.Separator, separatorCss);
export const SelectGroup = Primitive.Group;
-const StyledItem = styled(Primitive.Item, itemCss);
+const StyledItem = styled(Primitive.Item, itemCss, {
+ // we don't want to transform text using CSS in case of the Select,
+ // we want getLabel to handle it
+ textTransform: "none",
+});
const StyledIndicator = styled(Primitive.ItemIndicator, itemIndicatorCss);
diff --git a/packages/project-build/src/schema/props.ts b/packages/project-build/src/schema/props.ts
index a7755726b8..880b254870 100644
--- a/packages/project-build/src/schema/props.ts
+++ b/packages/project-build/src/schema/props.ts
@@ -33,7 +33,13 @@ export const Prop = z.union([
z.object({
...baseProp,
type: z.literal("page"),
- value: z.string(), // page id
+ value: z.union([
+ z.string(), // page id
+ z.object({
+ pageId: z.string(),
+ instanceId: z.string(),
+ }),
+ ]),
}),
z.object({
...baseProp,
diff --git a/packages/react-sdk/src/app/custom-components/shared/remix-link.tsx b/packages/react-sdk/src/app/custom-components/shared/remix-link.tsx
index 0793d5c45a..427c0f8d49 100644
--- a/packages/react-sdk/src/app/custom-components/shared/remix-link.tsx
+++ b/packages/react-sdk/src/app/custom-components/shared/remix-link.tsx
@@ -12,7 +12,11 @@ export const wrapLinkComponent = (BaseLink: LinkComponent) => {
const href = usePropUrl(getInstanceIdFromComponentProps(props), "href");
if (href?.type === "page") {
- return <RemixLink {...props} to={href.page.path} ref={ref} />;
+ let to = href.page.path;
+ if (href.hash !== undefined) {
+ to += `#${href.hash}`;
+ }
+ return <RemixLink {...props} to={to} ref={ref} />;
}
return <BaseLink {...props} ref={ref} />;
diff --git a/packages/react-sdk/src/components/link.tsx b/packages/react-sdk/src/components/link.tsx
index 8b2c83b551..2418af3138 100644
--- a/packages/react-sdk/src/components/link.tsx
+++ b/packages/react-sdk/src/components/link.tsx
@@ -19,10 +19,14 @@ export const Link = forwardRef<HTMLAnchorElement, Props>((props, ref) => {
const { assetBaseUrl } = getParams();
- let url: string | undefined;
+ let url = "#";
+
switch (href?.type) {
case "page":
url = href.page.path;
+ if (href.hash !== undefined) {
+ url += `#${href.hash}`;
+ }
break;
case "asset":
url = `${assetBaseUrl}${href.asset.name}`;
diff --git a/packages/react-sdk/src/props.test.ts b/packages/react-sdk/src/props.test.ts
index 6ada392502..4cea4341b4 100644
--- a/packages/react-sdk/src/props.test.ts
+++ b/packages/react-sdk/src/props.test.ts
@@ -56,6 +56,22 @@ describe("resolveUrlProp", () => {
value: page1.id,
};
+ const instnaceIdProp: Prop = {
+ type: "string",
+ id: unique(),
+ instanceId: unique(),
+ name: "id",
+ value: unique(),
+ };
+
+ const pageSectionProp: Prop = {
+ type: "page",
+ id: unique(),
+ instanceId,
+ name: unique(),
+ value: { pageId: page1.id, instanceId: instnaceIdProp.instanceId },
+ };
+
const pageByPathProp: Prop = {
type: "string",
id: unique(),
@@ -73,7 +89,17 @@ describe("resolveUrlProp", () => {
};
const props: PropsByInstanceId = new Map([
- [instanceId, [pageByIdProp, pageByPathProp, arbitraryUrlProp, assetProp]],
+ [
+ instanceId,
+ [
+ pageByIdProp,
+ pageByPathProp,
+ arbitraryUrlProp,
+ assetProp,
+ pageSectionProp,
+ ],
+ ],
+ [instnaceIdProp.instanceId, [instnaceIdProp]],
]);
const pages: Pages = new Map([
@@ -116,6 +142,15 @@ describe("resolveUrlProp", () => {
});
});
+ test("section on a page", () => {
+ expect(resolveUrlProp(instanceId, pageSectionProp.name, stores)).toEqual({
+ type: "page",
+ page: page1,
+ instanceId: instnaceIdProp.instanceId,
+ hash: instnaceIdProp.value,
+ });
+ });
+
test("arbitrary url", () => {
expect(resolveUrlProp(instanceId, arbitraryUrlProp.name, stores)).toEqual({
type: "string",
diff --git a/packages/react-sdk/src/props.ts b/packages/react-sdk/src/props.ts
index 5572f7b81e..e37eaee190 100644
--- a/packages/react-sdk/src/props.ts
+++ b/packages/react-sdk/src/props.ts
@@ -32,7 +32,7 @@ export const useInstanceProps = (instanceId: Instance["id"]) => {
const instancePropsObject: Record<Prop["name"], Prop["value"]> = {};
if (instanceProps) {
for (const prop of instanceProps) {
- if (prop.type !== "asset") {
+ if (prop.type !== "asset" && prop.type !== "page") {
instancePropsObject[prop.name] = prop.value;
}
}
@@ -74,7 +74,12 @@ export const resolveUrlProp = (
assets,
}: { props: PropsByInstanceId; pages: Pages; assets: Assets }
):
- | { type: "page"; page: Page }
+ | {
+ type: "page";
+ page: Page;
+ instanceId?: Instance["id"];
+ hash?: string;
+ }
| { type: "asset"; asset: Asset }
| { type: "string"; url: string }
| undefined => {
@@ -88,8 +93,30 @@ export const resolveUrlProp = (
}
if (prop.type === "page") {
- const page = pages.get(prop.value);
- return page && { type: "page", page };
+ if (typeof prop.value === "string") {
+ const page = pages.get(prop.value);
+ return page && { type: "page", page };
+ }
+
+ const { instanceId, pageId } = prop.value;
+
+ const page = pages.get(pageId);
+
+ if (page === undefined) {
+ return;
+ }
+
+ const idProp = props.get(instanceId)?.find((prop) => prop.name === "id");
+
+ return {
+ type: "page",
+ page,
+ instanceId,
+ hash:
+ idProp === undefined || idProp.type !== "string"
+ ? undefined
+ : idProp.value,
+ };
}
if (prop.type === "string") {
@@ -115,7 +142,7 @@ export const resolveUrlProp = (
export const usePropUrl = (instanceId: Instance["id"], name: string) => {
const { propsByInstanceIdStore, pagesStore, assetsStore } =
useContext(ReactSdkContext);
- const pageStore = useMemo(
+ const store = useMemo(
() =>
computed(
[propsByInstanceIdStore, pagesStore, assetsStore],
@@ -124,7 +151,7 @@ export const usePropUrl = (instanceId: Instance["id"], name: string) => {
),
[propsByInstanceIdStore, pagesStore, assetsStore, instanceId, name]
);
- return useStore(pageStore);
+ return useStore(store);
};
export const getInstanceIdFromComponentProps = (