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
2 changes: 2 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ consider additional positioning prop support on a case-by-case basis.
#### @zendeskgarden/react-chrome

- Removed `PRODUCT` type export. Use `IHeaderItemProps['product']` instead.
- Removed `hasFooter` prop for `Body` (no replacement needed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call out that IBodyProps is no longer exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good call.

- The following React component types have changed:
- Removed `IBodyProps` type export.
- Renamed `ICollapsibleSubNavItemProps` type export to `ISubNavCollapsibleItemProps`.
- `Header.ItemIcon`: `HTMLAttributes<HTMLElement>` -> `SVGAttributes<SVGElement>`
- `Nav.ItemIcon`: `HTMLAttributes<HTMLElement>` -> `SVGAttributes<SVGElement>`
Expand Down
2 changes: 1 addition & 1 deletion packages/chrome/demo/stories/ChromeStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const ChromeStory: Story<IArgs> = ({
)}
</SubNav>
)}
<Body hasFooter={hasFooter}>
<Body>
{hasHeader && (
<Header isStandalone={!(hasNav || hasSubNav)}>
{hasLogo && (
Expand Down
30 changes: 15 additions & 15 deletions packages/chrome/src/elements/body/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { IBodyProps } from '../../types';
import React, { HTMLAttributes, useMemo, useState } from 'react';
import { StyledBody } from '../../styled';
import { BodyContext } from '../../utils/useBodyContext';

/**
* @extends HTMLAttributes<HTMLDivElement>
*/
export const Body = React.forwardRef<HTMLDivElement, IBodyProps>(({ hasFooter, ...props }, ref) => {
const bodyContextValue = useMemo(() => ({ hasFooter: !!hasFooter }), [hasFooter]);
export const Body = React.forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
(props, ref) => {
const [hasFooter, setHasFooter] = useState(false);
const bodyContextValue = useMemo(
() => ({ hasFooter, setHasFooter }),
[hasFooter, setHasFooter]
);

return (
<BodyContext.Provider value={bodyContextValue}>
<StyledBody ref={ref} {...props} />
</BodyContext.Provider>
);
});
return (
<BodyContext.Provider value={bodyContextValue}>
<StyledBody ref={ref} {...props} />
</BodyContext.Provider>
);
}
);

Body.displayName = 'Body';

Body.propTypes = {
hasFooter: PropTypes.bool
};
9 changes: 7 additions & 2 deletions packages/chrome/src/elements/body/Content.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import React from 'react';
import { render } from 'garden-test-utils';
import { Content } from './Content';
import { Body } from './Body';

describe('Content', () => {
it('passes ref to underlying DOM element', () => {
const ref = React.createRef<HTMLDivElement>();
const { container } = render(<Content ref={ref} />);
const { queryByTestId } = render(
<Body>
<Content ref={ref} data-test-id="content" />
</Body>
);

expect(container.firstChild).toBe(ref.current);
expect(queryByTestId('content')).toBe(ref.current);
});
});
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 @@ -14,7 +14,7 @@ import { useBodyContext } from '../../utils/useBodyContext';
*/
export const Content = React.forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
(props, ref) => {
const { hasFooter } = useBodyContext();
const { hasFooter } = useBodyContext() || {};

return <StyledContent ref={ref} hasFooter={hasFooter} {...props} />;
}
Expand Down
21 changes: 19 additions & 2 deletions packages/chrome/src/elements/footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { HTMLAttributes } from 'react';
import React, { HTMLAttributes, useEffect } from 'react';
import { StyledFooter } from '../../styled';
import { useBodyContext } from '../../utils/useBodyContext';
import { FooterItem } from './FooterItem';

/**
* @extends HTMLAttributes<HTMLElement>
*/
export const FooterComponent = React.forwardRef<HTMLElement, HTMLAttributes<HTMLElement>>(
(props, ref) => <StyledFooter ref={ref} {...props} />
(props, ref) => {
const { hasFooter, setHasFooter } = useBodyContext() || {};

useEffect(() => {
if (!hasFooter && setHasFooter) {
setHasFooter(true);
}

return () => {
if (hasFooter && setHasFooter) {
setHasFooter(false);
}
};
}, [hasFooter, setHasFooter]);

return <StyledFooter ref={ref} {...props} />;
}
);

FooterComponent.displayName = 'Footer';
Expand Down
1 change: 0 additions & 1 deletion packages/chrome/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export {
PRODUCTS,
type IChromeProps,
type ISkipNavProps,
type IBodyProps,
type IHeaderProps,
type IHeaderItemProps,
type IHeaderItemTextProps,
Expand Down
5 changes: 0 additions & 5 deletions packages/chrome/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ export interface ISkipNavProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
zIndex?: number;
}

export interface IBodyProps extends HTMLAttributes<HTMLDivElement> {
/** Adjusts the body content height to allow space for a footer component */
hasFooter?: boolean;
}

export interface IHeaderProps extends HTMLAttributes<HTMLElement> {
/** Displays logo for standlone usage */
isStandalone?: boolean;
Expand Down
5 changes: 2 additions & 3 deletions packages/chrome/src/utils/useBodyContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import React, { useContext } from 'react';

interface IBodyContext {
hasFooter: boolean;
setHasFooter: (footerPresent: boolean) => void;
}

export const BodyContext = React.createContext<IBodyContext>({
hasFooter: true
});
export const BodyContext = React.createContext<IBodyContext | undefined>(undefined);

export const useBodyContext = () => {
return useContext(BodyContext);
Expand Down