Skip to content

Commit

Permalink
refactor(chrome): use transient props where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
ze-flo committed Oct 9, 2024
1 parent aae638c commit 2a964f0
Show file tree
Hide file tree
Showing 30 changed files with 174 additions and 152 deletions.
2 changes: 1 addition & 1 deletion packages/chrome/src/elements/SkipNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { StyledSkipNav, StyledSkipNavIcon } from '../styled';
*/
export const SkipNav = React.forwardRef<HTMLAnchorElement, ISkipNavProps>(
({ targetId, zIndex, children, ...props }, ref) => (
<StyledSkipNav href={`#${targetId}`} zIndex={zIndex} ref={ref} {...props}>
<StyledSkipNav href={`#${targetId}`} $zIndex={zIndex} ref={ref} {...props}>
<StyledSkipNavIcon />
{children}
</StyledSkipNav>
Expand Down
2 changes: 1 addition & 1 deletion packages/chrome/src/elements/body/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const Content = React.forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivEl
(props, ref) => {
const { hasFooter } = useBodyContext() || {};

return <StyledContent ref={ref} hasFooter={hasFooter} {...props} />;
return <StyledContent ref={ref} $hasFooter={hasFooter} {...props} />;
}
);

Expand Down
8 changes: 5 additions & 3 deletions packages/chrome/src/elements/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { HeaderItemIcon } from './HeaderItemIcon';
import { HeaderItemText } from './HeaderItemText';
import { HeaderItemWrapper } from './HeaderItemWrapper';

export const HeaderComponent = React.forwardRef<HTMLElement, IHeaderProps>((props, ref) => (
<StyledHeader ref={ref} {...props} />
));
export const HeaderComponent = React.forwardRef<HTMLElement, IHeaderProps>(
({ isStandalone, ...rest }, ref) => (
<StyledHeader ref={ref} $isStandalone={isStandalone} {...rest} />
)
);

HeaderComponent.displayName = 'Header';

Expand Down
15 changes: 12 additions & 3 deletions packages/chrome/src/elements/header/HeaderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ import { StyledHeaderItem, StyledLogoHeaderItem } from '../../styled';
* @extends ButtonHTMLAttributes<HTMLButtonElement>
*/
export const HeaderItem = React.forwardRef<HTMLButtonElement, IHeaderItemProps>(
({ hasLogo, product, ...other }, ref) => {
({ hasLogo, isRound, maxX, maxY, product, ...rest }, ref) => {
if (hasLogo) {
return <StyledLogoHeaderItem ref={ref} product={product} {...other} />;
return (
<StyledLogoHeaderItem
ref={ref}
$isRound={isRound}
$maxX={maxX}
$maxY={maxY}
$product={product}
{...rest}
/>
);
}

return <StyledHeaderItem ref={ref} {...other} />;
return <StyledHeaderItem ref={ref} $isRound={isRound} $maxX={maxX} $maxY={maxY} {...rest} />;
}
);

Expand Down
8 changes: 5 additions & 3 deletions packages/chrome/src/elements/header/HeaderItemText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import { StyledHeaderItemText } from '../../styled';
*
* @extends HTMLAttributes<HTMLSpanElement>
*/
export const HeaderItemText = React.forwardRef<HTMLElement, IHeaderItemTextProps>((props, ref) => (
<StyledHeaderItemText ref={ref} {...props} />
));
export const HeaderItemText = React.forwardRef<HTMLElement, IHeaderItemTextProps>(
({ isClipped, ...rest }, ref) => (
<StyledHeaderItemText ref={ref} $isClipped={isClipped} {...rest} />
)
);

HeaderItemText.displayName = 'Header.ItemText';

Expand Down
34 changes: 18 additions & 16 deletions packages/chrome/src/elements/nav/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ import { NavItemIcon } from './NavItemIcon';
import { NavItemText } from './NavItemText';
import { NavList } from './NavList';

export const NavComponent = React.forwardRef<HTMLElement, INavProps>((props, ref) => {
const { hue, isLight } = useChromeContext();
const navContextValue = useMemo(() => ({ isExpanded: !!props.isExpanded }), [props.isExpanded]);
export const NavComponent = React.forwardRef<HTMLElement, INavProps>(
({ isExpanded, ...rest }, ref) => {
const { hue, isLight } = useChromeContext();
const navContextValue = useMemo(() => ({ isExpanded: !!isExpanded }), [isExpanded]);

return (
<ThemeProvider
theme={parentTheme => ({
...parentTheme,
colors: { ...parentTheme.colors, base: isLight ? 'light' : 'dark' }
})}
>
<NavContext.Provider value={navContextValue}>
<StyledNav ref={ref} {...props} hue={hue} />
</NavContext.Provider>
</ThemeProvider>
);
});
return (
<ThemeProvider
theme={parentTheme => ({
...parentTheme,
colors: { ...parentTheme.colors, base: isLight ? 'light' : 'dark' }
})}
>
<NavContext.Provider value={navContextValue}>
<StyledNav ref={ref} $isExpanded={isExpanded} $hue={hue} {...rest} />
</NavContext.Provider>
</ThemeProvider>
);
}
);

NavComponent.displayName = 'Nav';

Expand Down
6 changes: 3 additions & 3 deletions packages/chrome/src/elements/nav/NavItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export const NavItem = React.forwardRef<HTMLButtonElement, INavItemProps>(
let retVal;

if (hasLogo) {
retVal = <StyledLogoNavItem ref={ref} hue={hue} product={product} {...other} />;
retVal = <StyledLogoNavItem ref={ref} $hue={hue} $product={product} {...other} />;
} else if (hasBrandmark) {
retVal = <StyledBrandmarkNavItem ref={ref} {...other} />;
} else {
retVal = (
<StyledNavButton
tabIndex={0}
ref={ref}
isExpanded={isExpanded}
hue={hue}
$isExpanded={isExpanded}
$hue={hue}
aria-current={isCurrent || undefined}
{...other}
/>
Expand Down
12 changes: 8 additions & 4 deletions packages/chrome/src/elements/nav/NavItemText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import { useNavContext } from '../../utils/useNavContext';
*
* @extends HTMLAttributes<HTMLSpanElement>
*/
export const NavItemText = React.forwardRef<HTMLElement, INavItemTextProps>((props, ref) => {
const { isExpanded } = useNavContext();
export const NavItemText = React.forwardRef<HTMLElement, INavItemTextProps>(
({ isWrapped, ...rest }, ref) => {
const { isExpanded } = useNavContext();

return <StyledNavItemText ref={ref} isExpanded={isExpanded} {...props} />;
});
return (
<StyledNavItemText ref={ref} $isExpanded={isExpanded} $isWrapped={isWrapped} {...rest} />
);
}
);

NavItemText.displayName = 'Nav.ItemText';

Expand Down
16 changes: 8 additions & 8 deletions packages/chrome/src/elements/sheet/Sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ const SheetComponent = React.forwardRef<HTMLElement, ISheetProps>(
<SheetContext.Provider value={sheetContext}>
<StyledSheet
inert={isOpen ? undefined : ''}
isOpen={isOpen}
isAnimated={isAnimated}
placement={placement}
size={size}
$isOpen={isOpen}
$isAnimated={isAnimated}
$placement={placement}
$size={size}
tabIndex={-1}
id={idPrefix}
aria-labelledby={titleId}
Expand All @@ -64,10 +64,10 @@ const SheetComponent = React.forwardRef<HTMLElement, ISheetProps>(
{...props}
>
<StyledSheetWrapper
isOpen={isOpen}
isAnimated={isAnimated}
placement={placement}
size={size}
$isOpen={isOpen}
$isAnimated={isAnimated}
$placement={placement}
$size={size}
>
{children}
</StyledSheetWrapper>
Expand Down
4 changes: 2 additions & 2 deletions packages/chrome/src/elements/sheet/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import React, { forwardRef } from 'react';
import { ISheetFooterProps } from '../../../types';
import { StyledSheetFooter } from '../../../styled';

const SheetFooter = forwardRef<HTMLElement, ISheetFooterProps>((props, ref) => {
return <StyledSheetFooter ref={ref} {...props} />;
const SheetFooter = forwardRef<HTMLElement, ISheetFooterProps>(({ isCompact, ...rest }, ref) => {
return <StyledSheetFooter ref={ref} $isCompact={isCompact} {...rest} />;
});

SheetFooter.displayName = 'Sheet.Footer';
Expand Down
2 changes: 1 addition & 1 deletion packages/chrome/src/elements/sheet/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useSheetContext } from '../../../utils/useSheetContext';
const SheetHeader = forwardRef<HTMLElement, HTMLAttributes<HTMLElement>>((props, ref) => {
const { isCloseButtonPresent } = useSheetContext();

return <StyledSheetHeader ref={ref} isCloseButtonPresent={isCloseButtonPresent} {...props} />;
return <StyledSheetHeader ref={ref} $isCloseButtonPresent={isCloseButtonPresent} {...props} />;
});

SheetHeader.displayName = 'Sheet.Header';
Expand Down
4 changes: 2 additions & 2 deletions packages/chrome/src/styled/StyledSkipNav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const sizeStyles = ({ theme }: ThemeProps<DefaultTheme>) => {
};

interface IStyledSkipNavProps {
zIndex?: number;
$zIndex?: number;
}

/*
Expand All @@ -109,7 +109,7 @@ export const StyledSkipNav = styled.a.attrs({
justify-content: center;
transform: translateX(-50%);
direction: ${props => props.theme.rtl && 'rtl'};
z-index: ${props => props.zIndex};
z-index: ${props => props.$zIndex};
border-radius: ${props => props.theme.borderRadii.md};
text-decoration: underline;
white-space: nowrap;
Expand Down
6 changes: 3 additions & 3 deletions packages/chrome/src/styled/body/StyledContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { getFooterHeight, getHeaderHeight } from '../utils';
const COMPONENT_ID = 'chrome.content';

interface IStyledContentProps {
hasFooter?: boolean;
$hasFooter?: boolean;
}

const sizeStyles = ({ theme, hasFooter }: IStyledContentProps & ThemeProps<DefaultTheme>) => {
const sizeStyles = ({ theme, $hasFooter }: IStyledContentProps & ThemeProps<DefaultTheme>) => {
const fontSize = theme.fontSizes.md;
const height = hasFooter
const height = $hasFooter
? `calc(100% - ${math(`${getHeaderHeight(theme)} + ${getFooterHeight(theme)}`)})`
: `calc(100% - ${getHeaderHeight(theme)})`;
const lineHeight = getLineHeight(theme.lineHeights.md, theme.fontSizes.md);
Expand Down
20 changes: 10 additions & 10 deletions packages/chrome/src/styled/header/StyledBaseHeaderItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ import { getHeaderItemSize } from '../utils';
const COMPONENT_ID = 'chrome.base_header_item';

export interface IStyledBaseHeaderItemProps {
maxX?: boolean;
maxY?: boolean;
isRound?: boolean;
$maxX?: boolean;
$maxY?: boolean;
$isRound?: boolean;
}

/*
* 1. Button element reset
*/
const sizeStyles = ({
theme,
maxY,
isRound
$maxY,
$isRound
}: IStyledBaseHeaderItemProps & ThemeProps<DefaultTheme>) => {
const margin = `0 ${theme.space.base * 3}px`;
const size = getHeaderItemSize(theme);
const height = maxY ? '100%' : size;
const height = $maxY ? '100%' : size;
const lineHeight = getLineHeight(size, theme.fontSizes.md);
const padding = `0 ${theme.space.base * 0.75}px`;
let borderRadius;

if (isRound) {
if ($isRound) {
borderRadius = '100%';
} else if (maxY) {
} else if ($maxY) {
borderRadius = '0';
} else {
borderRadius = theme.borderRadii.md;
Expand All @@ -61,9 +61,9 @@ export const StyledBaseHeaderItem = styled.button.attrs({
})<IStyledBaseHeaderItemProps>`
display: inline-flex;
position: relative;
flex: ${props => props.maxX && '1'};
flex: ${props => props.$maxX && '1'};
align-items: center;
justify-content: ${props => (props.maxX ? 'start' : 'center')};
justify-content: ${props => (props.$maxX ? 'start' : 'center')};
order: 1;
transition:
box-shadow 0.1s ease-in-out,
Expand Down
10 changes: 5 additions & 5 deletions packages/chrome/src/styled/header/StyledHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { getHeaderHeight } from '../utils';
const COMPONENT_ID = 'chrome.header';

export interface IStyledHeaderProps {
isStandalone?: boolean;
$isStandalone?: boolean;
}

const colorStyles = ({ theme, isStandalone }: IStyledHeaderProps & ThemeProps<DefaultTheme>) => {
const colorStyles = ({ theme, $isStandalone }: IStyledHeaderProps & ThemeProps<DefaultTheme>) => {
const backgroundColor = getColor({ theme, variable: 'background.default' });
const borderColor = getColor({ theme, variable: 'border.default' });
const boxShadowColor = getColor({ variable: 'shadow.small', theme });
const boxShadow = isStandalone
const boxShadow = $isStandalone
? theme.shadows.lg('0', `${theme.space.base * 2}px`, boxShadowColor)
: undefined;
const foregroundColor = getColor({ theme, variable: 'foreground.subtle' });
Expand Down Expand Up @@ -53,7 +53,7 @@ export const StyledHeader = styled.header.attrs<IStyledHeaderProps>({
'data-garden-version': PACKAGE_VERSION
})<IStyledHeaderProps>`
display: flex;
position: ${props => props.isStandalone && 'relative'};
position: ${props => props.$isStandalone && 'relative'};
align-items: center;
justify-content: flex-end;
Expand All @@ -62,7 +62,7 @@ export const StyledHeader = styled.header.attrs<IStyledHeaderProps>({
${colorStyles};
${StyledLogoHeaderItem} {
display: ${props => props.isStandalone && 'inline-flex'};
display: ${props => props.$isStandalone && 'inline-flex'};
}
${props => retrieveComponentStyles(COMPONENT_ID, props)};
Expand Down
8 changes: 4 additions & 4 deletions packages/chrome/src/styled/header/StyledHeaderItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const COMPONENT_ID = 'chrome.header_item';
/*
* 1. Anchor reset.
*/
const colorStyles = ({ theme, maxY }: IStyledBaseHeaderItemProps & ThemeProps<DefaultTheme>) => {
const colorStyles = ({ theme, $maxY }: IStyledBaseHeaderItemProps & ThemeProps<DefaultTheme>) => {
const options = { theme, variable: 'foreground.subtle' };
const hoverColor = getColor({ ...options, dark: { offset: -100 }, light: { offset: 100 } });
const activeColor = getColor({ ...options, dark: { offset: -200 }, light: { offset: 200 } });
Expand All @@ -29,7 +29,7 @@ const colorStyles = ({ theme, maxY }: IStyledBaseHeaderItemProps & ThemeProps<De
color: inherit; /* [1] */
}
${focusStyles({ theme, inset: maxY })};
${focusStyles({ theme, inset: $maxY })};
/* prettier-ignore */
&:hover ${StyledHeaderItemIcon},
Expand All @@ -45,8 +45,8 @@ const colorStyles = ({ theme, maxY }: IStyledBaseHeaderItemProps & ThemeProps<De
`;
};

const sizeStyles = ({ theme, isRound }: IStyledBaseHeaderItemProps & ThemeProps<DefaultTheme>) => {
const iconBorderRadius = isRound ? '100px' : undefined;
const sizeStyles = ({ theme, $isRound }: IStyledBaseHeaderItemProps & ThemeProps<DefaultTheme>) => {
const iconBorderRadius = $isRound ? '100px' : undefined;
const imageBorderRadius = math(`${theme.borderRadii.md} - 1`);
const imageSize = math(`${getHeaderItemSize(theme)} - ${theme.space.base * 2}`);

Expand Down
4 changes: 2 additions & 2 deletions packages/chrome/src/styled/header/StyledHeaderItemText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { retrieveComponentStyles } from '@zendeskgarden/react-theming';
const COMPONENT_ID = 'chrome.header_item_text';

export interface IStyledHeaderItemTextProps {
isClipped?: boolean;
$isClipped?: boolean;
}

export const StyledHeaderItemText = styled.span.attrs<IStyledHeaderItemTextProps>({
Expand All @@ -21,7 +21,7 @@ export const StyledHeaderItemText = styled.span.attrs<IStyledHeaderItemTextProps
})<IStyledHeaderItemTextProps>`
margin: ${props => `0 ${props.theme.space.base * 0.75}px`};
${props => props.isClipped && hideVisually()}
${props => props.$isClipped && hideVisually()}
${props => retrieveComponentStyles(COMPONENT_ID, props)};
`;
9 changes: 6 additions & 3 deletions packages/chrome/src/styled/header/StyledLogoHeaderItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import { getNavWidth, getProductColor } from '../utils';
const COMPONENT_ID = 'chrome.header_item';

export interface IStyledLogoHeaderItemProps {
product?: Product;
$product?: Product;
}

const colorStyles = ({ theme, product }: IStyledLogoHeaderItemProps & ThemeProps<DefaultTheme>) => {
const colorStyles = ({
theme,
$product
}: IStyledLogoHeaderItemProps & ThemeProps<DefaultTheme>) => {
const borderColor = getColor({ theme, variable: 'border.default' });
const fill = getColor({ theme, variable: 'foreground.default' });
const color = getProductColor(product, fill /* fallback */);
const color = getProductColor($product, fill /* fallback */);

return css`
border-${theme.rtl ? 'left' : 'right'}-color: ${borderColor};
Expand Down
Loading

0 comments on commit 2a964f0

Please sign in to comment.