Skip to content

Commit

Permalink
refactor(SSR): get rid of deepHas
Browse files Browse the repository at this point in the history
... because deepHas has issues with SSR
  • Loading branch information
Lisa18289 authored Jan 23, 2025
1 parent d9770bf commit 0a8e879
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 72 deletions.
5 changes: 2 additions & 3 deletions packages/components/src/components/Avatar/Avatar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@

/* Variants */
@mixin color($color) {
&.dynamic-#{$color}:has(.initials),
&.#{$color} {
--background-color: var(--decorative--#{$color}-background-color);
--content-color: var(--decorative--#{$color}-content-color);
--content-accent-color: var(
--decorative--#{$color}-content-accent-color
);
--content-accent-color: var(--decorative--#{$color}-content-accent-color);
}
}

Expand Down
7 changes: 2 additions & 5 deletions packages/components/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { getColorFromChildren } from "@/components/Avatar/lib/getColorFromChildr
import type { PropsWithClassName } from "@/lib/types/props";
import type { FlowComponentProps } from "@/lib/componentFactory/flowComponent";
import { flowComponent } from "@/lib/componentFactory/flowComponent";
import { deepHas } from "@/lib/react/deepHas";
import { Initials } from "@/components/Initials";

export const avatarColors = [
"blue",
Expand All @@ -33,13 +31,12 @@ export interface AvatarProps
export const Avatar = flowComponent("Avatar", (props) => {
const { children, className, color, size = "m", refProp: ref } = props;

const hasInitials = deepHas(children, Initials);

const rootClassName = clsx(
styles.avatar,
styles[`size-${size}`],
className,
styles[color ?? (hasInitials ? getColorFromChildren(children) : "blue")],
styles[color ?? "blue"],
!color && styles[`dynamic-${getColorFromChildren(children)}`],
);

const propsContext: PropsContext = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
:global(.flow--checkbox-button) {
width: auto;
}

&:empty {
display: none;
}
}
22 changes: 7 additions & 15 deletions packages/components/src/components/CheckboxGroup/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import clsx from "clsx";
import type { PropsContext } from "@/lib/propsContext";
import { PropsContextProvider } from "@/lib/propsContext";
import { FieldError } from "@/components/FieldError";
import { CheckboxButton } from "@/components/CheckboxButton";
import { TunnelExit, TunnelProvider } from "@mittwald/react-tunnel";
import formFieldStyles from "../FormField/FormField.module.scss";
import type { ColumnLayoutProps } from "@/components/ColumnLayout";
import { ColumnLayout } from "@/components/ColumnLayout";
import type { FlowComponentProps } from "@/lib/componentFactory/flowComponent";
import { flowComponent } from "@/lib/componentFactory/flowComponent";
import { deepHas } from "@/lib/react/deepHas";

export interface CheckboxGroupProps
extends PropsWithChildren<Omit<Aria.CheckboxGroupProps, "children">>,
Expand Down Expand Up @@ -41,29 +39,23 @@ export const CheckboxGroup = flowComponent("CheckboxGroup", (props) => {
tunnelId: "checkboxes",
},
CheckboxButton: {
tunnelId: "checkboxes",
tunnelId: "checkboxButtons",
},
};

const hasCheckboxButtons = deepHas(children, CheckboxButton);

return (
<Aria.CheckboxGroup {...rest} className={rootClassName} ref={ref}>
<PropsContextProvider props={propsContext}>
<TunnelProvider>
{children}

{hasCheckboxButtons && (
<ColumnLayout s={s} m={m} l={l} className={styles.checkboxGroup}>
<TunnelExit id="checkboxes" />
</ColumnLayout>
)}
<ColumnLayout s={s} m={m} l={l} className={styles.checkboxGroup}>
<TunnelExit id="checkboxButtons" />
</ColumnLayout>

{!hasCheckboxButtons && (
<div className={styles.checkboxGroup}>
<TunnelExit id="checkboxes" />
</div>
)}
<div className={styles.checkboxGroup}>
<TunnelExit id="checkboxes" />
</div>

<TunnelExit id="fieldDescription" />
<TunnelExit id="fieldError" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.columnLayoutContainer {
container-type: inline-size;

&:has(.columnLayout:empty) {
display: none;
}
}

.columnLayout {
Expand Down
11 changes: 1 addition & 10 deletions packages/components/src/components/MenuItem/MenuItemContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import {
IconRadioOff,
IconRadioOn,
} from "@/components/Icon/components/icons";
import { Text } from "@/components/Text";
import { deepHas } from "@/lib/react/deepHas";
import { Wrap } from "@/components/Wrap";
import clsx from "clsx";
import { Avatar } from "@/components/Avatar";
import { Switch } from "@/components/Switch";

interface Props extends Aria.MenuItemRenderProps, PropsWithChildren {
Expand Down Expand Up @@ -67,18 +63,13 @@ export const MenuItemContent: FC<Props> = (props) => {
<IconCheckboxEmpty />
);

const hasText = deepHas(children, Text);
const hasAvatar = deepHas(children, Avatar);

return (
<>
<PropsContextProvider props={controlIconPropsContext}>
{selectionIcon}
</PropsContextProvider>
<PropsContextProvider props={propsContext}>
<Wrap if={!hasText && !hasAvatar}>
<Text>{children}</Text>
</Wrap>
{children}
</PropsContextProvider>
</>
);
Expand Down
14 changes: 6 additions & 8 deletions packages/components/src/components/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import type { ComponentProps, FC, PropsWithChildren } from "react";
import React from "react";
import styles from "./Navigation.module.scss";
import clsx from "clsx";
import { NavigationGroup } from "@/components/Navigation";
import { Wrap } from "@/components/Wrap";
import type { PropsContext } from "@/lib/propsContext";
import { PropsContextProvider } from "@/lib/propsContext";
import type { PropsWithClassName } from "@/lib/types/props";
import { deepHas } from "@/lib/react/deepHas";
import { TunnelExit } from "@mittwald/react-tunnel";

export interface NavigationProps
extends PropsWithChildren<ComponentProps<"nav">>,
Expand All @@ -18,8 +16,6 @@ export const Navigation: FC<NavigationProps> = (props) => {

const rootClassName = clsx(styles.navigation, className);

const hasGroups = deepHas(children, NavigationGroup);

const propsContext: PropsContext = {
Link: {
wrapWith: <li />,
Expand All @@ -31,15 +27,17 @@ export const Navigation: FC<NavigationProps> = (props) => {
Icon: {
className: styles.icon,
},
tunnelId: "links",
},
};

return (
<nav className={rootClassName} role="navigation" {...rest}>
<PropsContextProvider props={propsContext} mergeInParentContext>
<Wrap if={!hasGroups}>
<ul>{children}</ul>
</Wrap>
<ul>
<TunnelExit id="links" />
</ul>
{children}
</PropsContextProvider>
</nav>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const NavigationGroup: FC<NavigationGroupProps> = (props) => {
"aria-hidden": true,
},
Link: {
tunnelId: "links",
tunnelId: "groupLinks",
},
};

Expand All @@ -43,7 +43,7 @@ export const NavigationGroup: FC<NavigationGroupProps> = (props) => {
{children}
<Content clearPropsContext={false}>
<ul>
<TunnelExit id="links" />
<TunnelExit id="groupLinks" />
</ul>
</Content>
</Accordion>
Expand All @@ -64,7 +64,7 @@ export const NavigationGroup: FC<NavigationGroupProps> = (props) => {
{children}
<TunnelExit id="Label" />
<ul>
<TunnelExit id="links" />
<TunnelExit id="groupLinks" />
</ul>
</PropsContextProvider>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
display: flex;
flex-direction: column;
row-gap: var(--form-control--spacing-y);

&:empty {
display: none;
}
}
22 changes: 7 additions & 15 deletions packages/components/src/components/RadioGroup/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import type { ColumnLayoutProps } from "@/components/ColumnLayout";
import { ColumnLayout } from "@/components/ColumnLayout";
import { TunnelExit, TunnelProvider } from "@mittwald/react-tunnel";
import formFieldStyles from "../FormField/FormField.module.scss";
import RadioButton from "./components/RadioButton";
import type { FlowComponentProps } from "@/lib/componentFactory/flowComponent";
import { flowComponent } from "@/lib/componentFactory/flowComponent";
import { deepHas } from "@/lib/react/deepHas";

export interface RadioGroupProps
extends PropsWithChildren<Omit<Aria.RadioGroupProps, "children">>,
Expand Down Expand Up @@ -42,15 +40,13 @@ export const RadioGroup = flowComponent("RadioGroup", (props) => {
tunnelId: "fieldError",
},
RadioButton: {
tunnelId: "radios",
tunnelId: "radioButtons",
},
Radio: {
tunnelId: "radios",
},
};

const hasRadioButtons = deepHas(children, RadioButton);

return (
<Aria.RadioGroup {...rest} className={rootClassName} ref={ref}>
<TunnelProvider>
Expand All @@ -61,17 +57,13 @@ export const RadioGroup = flowComponent("RadioGroup", (props) => {
>
{children}

{hasRadioButtons && (
<ColumnLayout s={s} m={m} l={l} className={styles.radioGroup}>
<TunnelExit id="radios" />
</ColumnLayout>
)}
<ColumnLayout s={s} m={m} l={l} className={styles.radioGroup}>
<TunnelExit id="radioButtons" />
</ColumnLayout>

{!hasRadioButtons && (
<div className={styles.radioGroup}>
<TunnelExit id="radios" />
</div>
)}
<div className={styles.radioGroup}>
<TunnelExit id="radios" />
</div>

<TunnelExit id="fieldDescription" />
<TunnelExit id="fieldError" />
Expand Down
14 changes: 1 addition & 13 deletions packages/components/src/styles/mixins/menuItem.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,8 @@

/* Elements */
.icon {
order: 0;
color: var(--menu-item--icon-color--default);
}

.text {
order: 1;
}

.icon + .text {
margin-inline-start: var(--menu-item--spacing);
}

.switch {
order: 2;
margin-inline-end: var(--menu-item--spacing);
}

&:hover {
Expand Down

0 comments on commit 0a8e879

Please sign in to comment.