Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/strange-years-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ensembleui/react-kitchen-sink": patch
"@ensembleui/react-runtime": patch
---

prevent full app reload when using Button under Link widget
4 changes: 2 additions & 2 deletions apps/kitchen-sink/src/ensemble/screens/widgets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,8 @@ View:
color: blue
hoverColor: red
widget:
Text:
text: Forms Page (with hover effect)
Button:
label: Forms Page (with hover effect)

- Card:
styles:
Expand Down
23 changes: 22 additions & 1 deletion packages/runtime/src/widgets/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Expression, EnsembleAction } from "@ensembleui/react-framework";
import { useRegisterBindings, unwrapWidget } from "@ensembleui/react-framework";
import { useMemo, useCallback } from "react";
import { Link as RouterLink } from "react-router-dom";
import { Link as RouterLink, useNavigate } from "react-router-dom";
import { cloneDeep } from "lodash-es";
import { WidgetRegistry } from "../registry";
import type { EnsembleWidgetProps } from "../shared/types";
Expand Down Expand Up @@ -41,6 +41,7 @@ export type LinkProps = {

export const Link: React.FC<LinkProps> = ({ id, onTap, widget, ...rest }) => {
const action = useEnsembleAction(onTap);
const navigate = useNavigate();

const { values, rootRef } = useRegisterBindings({ ...rest, widgetName }, id);

Expand Down Expand Up @@ -92,6 +93,25 @@ export const Link: React.FC<LinkProps> = ({ id, onTap, widget, ...rest }) => {
);
}, [values?.url]);

// Intercept normal left-clicks (no modifiers) during capture phase
// This ensures navigation is handled client-side even if the child (e.g., Button)
// calls stopPropagation in its onClick. Right/middle clicks remain native.
const onClickCapture = useCallback(
(e: React.MouseEvent) => {
if (!values?.url || isExternalUrl) return;
// only handle left click without modifiers
if (e.button !== 0) return;
if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) return;

e.preventDefault();
navigate(values.url, {
replace: Boolean(values.replace),
state: values.inputs,
});
},
[navigate, values?.url, values?.replace, values?.inputs, isExternalUrl],
);

if (!values?.url) {
return (
<span ref={rootRef} style={linkStyles}>
Expand Down Expand Up @@ -130,6 +150,7 @@ export const Link: React.FC<LinkProps> = ({ id, onTap, widget, ...rest }) => {
// For internal navigation, use React Router Link
return (
<RouterLink
onClickCapture={onClickCapture}
onClick={handleClick}
onMouseEnter={(e): void => {
if (hoverStyles.color) e.currentTarget.style.color = hoverStyles.color;
Expand Down
29 changes: 29 additions & 0 deletions packages/runtime/src/widgets/__tests__/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,35 @@ describe("Link Widget", () => {

consoleSpy.mockRestore();
});

test("navigates on left-click when Link wraps Button (capture-phase)", async () => {
let currentLocation: ReturnType<typeof useLocation>;

const LocationTracker = (): null => {
const location = useLocation();
currentLocation = location;
return null;
};

render(
<MemoryRouter initialEntries={["/one/foo"]}>
<LocationTracker />
<Link
url="/two/abc"
widget={{
Button: { label: "Open Link" },
}}
/>
</MemoryRouter>,
);

const buttonText = screen.getByText("Open Link");
fireEvent.click(buttonText, { button: 0 });

await waitFor(() => {
expect(currentLocation.pathname).toBe("/two/abc");
});
});
});

describe("Link Widget Integration Tests", () => {
Expand Down