Skip to content

Commit d41a41a

Browse files
committed
avoid merging JSX.Elements in config
1 parent ba8ef6b commit d41a41a

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

packages/bento-design-system/src/BentoConfigContext.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createContext, useContext } from "react";
1+
import React, { createContext, useContext } from "react";
22
import * as defaultConfigs from "./util/defaultConfigs";
33
import type { BentoConfig, PartialBentoConfig } from "./BentoConfig";
4-
import { deepmerge } from "deepmerge-ts";
4+
import { deepmergeCustom } from "deepmerge-ts";
55
import { Children } from "./util/Children";
66

77
const BentoConfigContext = createContext<BentoConfig>(defaultConfigs);
@@ -23,6 +23,17 @@ export function BentoConfigProvider({
2323
// in case this is the top level provider.
2424
const parentConfig = useBentoConfig();
2525

26+
const deepmerge = deepmergeCustom({
27+
mergeRecords: (value, utils) => {
28+
// NOTE(vince): in case of a JSX.Element in the config (like the Navigation's activeVisualElement),
29+
// we don't want to merge the props of the two elements, but we want to take just the second element instead.
30+
if (React.isValidElement(value[0]) || React.isValidElement(value[1])) {
31+
return value[1];
32+
}
33+
return utils.actions.defaultMerge;
34+
},
35+
});
36+
2637
return (
2738
<BentoConfigContext.Provider value={deepmerge(parentConfig, config) as BentoConfig}>
2839
{children}

packages/storybook/stories/Components/Navigation.stories.tsx

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createComponentStories } from "../util";
22
import { Navigation } from "../";
3-
import { ComponentProps } from "react";
43
import { IconInformative, IllustrationIdea } from "..";
4+
import { Box, NavigationProps, withBentoConfig } from "@buildo/bento-design-system";
55

6-
const destinations: ComponentProps<typeof Navigation>["destinations"] = [
6+
const destinations: NavigationProps<"none">["destinations"] = [
77
{
88
label: "Destination 1",
99
href: "https://google.com",
@@ -47,3 +47,25 @@ export const withIllustrations = createControlledStory("destination1", {
4747
kind: "illustration",
4848
destinations: destinations.map((d) => ({ ...d, illustration: IllustrationIdea })) as any,
4949
});
50+
51+
// Note(vince): test that the nested activeVisualElement completely replace the parent one,
52+
// and their props are not merged, i.e. the resulting activeVisualElement
53+
const CustomNavigation = withBentoConfig(
54+
{
55+
navigation: {
56+
activeVisualElement: <Box background="brandPrimary" height={8} borderRadius={4} />,
57+
uppercaseLabel: true,
58+
},
59+
},
60+
withBentoConfig(
61+
{
62+
navigation: {
63+
activeVisualElement: <Box background="brandSecondary" height={8} />,
64+
},
65+
},
66+
Navigation
67+
)
68+
);
69+
export const withCustomActiveVisualElement = () => {
70+
return <CustomNavigation kind="none" destinations={destinations} size="large" />;
71+
};

0 commit comments

Comments
 (0)