-
Notifications
You must be signed in to change notification settings - Fork 119
feat(side-panel): create package #3827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1bb0bb5
feat(side-panel): create package
nkrantz 5748fd1
chore(side-panel): build side panel
nkrantz c0527d1
chore: prep for handoff
nkrantz 7356379
chore: overlay side panel at smaller 2 breakpoints
nkrantz ab54fa0
chore: more updates, fix padding size
nkrantz c1458bf
chore: use useTransitions instead of useSpring
nkrantz c74803e
chore: work on full comp story
nkrantz 4aa0a77
chore: make side panel sticky and full height
nkrantz db649d9
chore: small changes to api, scrollbar styles
nkrantz 4ba735b
chore: improvements
nkrantz d1362c1
chore: add tests
nkrantz bf4538a
chore: oops yarn lock update
nkrantz 0cd622a
chore: changesets
nkrantz 909314d
chore: improve a11y
nkrantz bbc06f1
chore: yarn lock
nkrantz 0ae55c6
chore: fix a11y storybook failure
nkrantz 8eca348
chore: improve API
nkrantz 9dbeca7
chore: fix tests etc
nkrantz 7abdf39
chore: design changes
nkrantz a8b970d
chore: small fix for testing with react 16 and 17
nkrantz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@twilio-paste/side-panel": major | ||
"@twilio-paste/core": minor | ||
--- | ||
|
||
[Side Panel] add new Side Panel component. Side panel is a container that pushes the main page content when open. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@twilio-paste/codemods": minor | ||
--- | ||
|
||
[codemods] add new package: Side Panel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/paste-core/components/side-panel/__tests__/index.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { act, render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { Button } from "@twilio-paste/button"; | ||
import { Theme } from "@twilio-paste/theme"; | ||
import * as React from "react"; | ||
|
||
import { | ||
SidePanel, | ||
SidePanelBody, | ||
SidePanelContainer, | ||
SidePanelHeader, | ||
SidePanelHeaderActions, | ||
SidePanelPushContentWrapper, | ||
} from "../src"; | ||
|
||
const MockSidePanel = ({ element = "SIDE_PANEL" }: { element?: string }): JSX.Element => { | ||
const [isOpen, setIsOpen] = React.useState(true); | ||
return ( | ||
<Theme.Provider theme="twilio" data-testid="wrapper"> | ||
<SidePanelContainer | ||
sidePanelId="side-panel-id" | ||
element={`${element}_CONTAINER`} | ||
setIsOpen={setIsOpen} | ||
isOpen={isOpen} | ||
> | ||
<SidePanel label="my side panel" element={element}> | ||
<SidePanelHeader element={`${element}_HEADER`}> | ||
Heading | ||
<SidePanelHeaderActions element={`${element}_HEADER_ACTIONS`}> | ||
<Button data-testid="action-button" variant="secondary_icon"> | ||
... | ||
</Button> | ||
</SidePanelHeaderActions> | ||
</SidePanelHeader> | ||
<SidePanelBody element={`${element}_BODY`}>Body</SidePanelBody> | ||
</SidePanel> | ||
<SidePanelPushContentWrapper element={`${element}_PUSH_CONTENT_WRAPPER`}> | ||
Page content | ||
</SidePanelPushContentWrapper> | ||
</SidePanelContainer> | ||
</Theme.Provider> | ||
); | ||
}; | ||
|
||
describe("SidePanel", () => { | ||
beforeEach(() => { | ||
render(<MockSidePanel />); | ||
}); | ||
it("should render", () => { | ||
expect(screen.getByRole("dialog")).toBeDefined(); | ||
}); | ||
it("should be removed from the DOM when isOpen=false", async () => { | ||
const closeButton = screen | ||
.getByRole("dialog") | ||
.querySelector('[data-paste-element="SIDE_PANEL_HEADER_CLOSE_BUTTON"]'); | ||
if (closeButton) await userEvent.click(closeButton); | ||
// r-t-l act() necessary for testing with react 17 | ||
await act(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
}); | ||
expect(screen.queryByRole("dialog")).toBeNull(); // queryByRole returns null if no element is found whereas getByRole throws an error | ||
}); | ||
it("should have an id", () => { | ||
expect(screen.getByRole("dialog").getAttribute("id")).toEqual("side-panel-id"); | ||
}); | ||
it("should have an aria label", () => { | ||
expect(screen.getByLabelText("my side panel")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe("Customization", () => { | ||
it("should set default element values", () => { | ||
render(<MockSidePanel />); | ||
const sidePanelWrapper = screen.getByTestId("wrapper"); | ||
const sidePanel = screen.getByRole("dialog"); | ||
expect(sidePanelWrapper.querySelector('[data-paste-element="SIDE_PANEL_CONTAINER"]')).toBeInTheDocument(); | ||
expect( | ||
sidePanelWrapper.querySelector('[data-paste-element="SIDE_PANEL_PUSH_CONTENT_WRAPPER"]'), | ||
).toBeInTheDocument(); | ||
expect(sidePanel.getAttribute("data-paste-element")).toEqual("SIDE_PANEL"); | ||
expect(sidePanel.querySelector('[data-paste-element="ANIMATED_SIDE_PANEL_WRAPPER"]')).toBeInTheDocument(); | ||
expect(sidePanel.querySelector('[data-paste-element="INNER_SIDE_PANEL"]')).toBeInTheDocument(); | ||
expect(screen.getByText("Heading").getAttribute("data-paste-element")).toBe("SIDE_PANEL_HEADER"); | ||
expect(screen.getByText("...").parentElement?.parentElement?.getAttribute("data-paste-element")).toBe( | ||
"SIDE_PANEL_HEADER_ACTIONS", | ||
); | ||
expect(screen.getByText("Body").getAttribute("data-paste-element")).toBe("SIDE_PANEL_BODY"); | ||
expect(screen.getByText("Page content").getAttribute("data-paste-element")).toBe("SIDE_PANEL_PUSH_CONTENT_WRAPPER"); | ||
}); | ||
|
||
it("should set custom element values", () => { | ||
render(<MockSidePanel element="FTP" />); | ||
const sidePanelWrapper = screen.getByTestId("wrapper"); | ||
const sidePanel = screen.getByRole("dialog"); | ||
expect(sidePanelWrapper.querySelector('[data-paste-element="FTP_CONTAINER"]')).toBeInTheDocument(); | ||
expect(sidePanelWrapper.querySelector('[data-paste-element="FTP_PUSH_CONTENT_WRAPPER"]')).toBeInTheDocument(); | ||
expect(sidePanel.getAttribute("data-paste-element")).toEqual("FTP"); | ||
expect(sidePanel.querySelector('[data-paste-element="ANIMATED_FTP_WRAPPER"]')).toBeInTheDocument(); | ||
expect(sidePanel.querySelector('[data-paste-element="INNER_FTP"]')).toBeInTheDocument(); | ||
expect(screen.getByText("Heading").getAttribute("data-paste-element")).toBe("FTP_HEADER"); | ||
expect(screen.getByText("...").parentElement?.parentElement?.getAttribute("data-paste-element")).toBe( | ||
"FTP_HEADER_ACTIONS", | ||
); | ||
expect(screen.getByText("Body").getAttribute("data-paste-element")).toBe("FTP_BODY"); | ||
expect(screen.getByText("Page content").getAttribute("data-paste-element")).toBe("FTP_PUSH_CONTENT_WRAPPER"); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { build } = require("../../../../tools/build/esbuild"); | ||
|
||
build(require("./package.json")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"name": "@twilio-paste/side-panel", | ||
"version": "0.0.0", | ||
"category": "layout", | ||
"status": "production", | ||
"description": "Side Panel is a container that pushes the main page content when open.", | ||
"author": "Twilio Inc.", | ||
"license": "MIT", | ||
"main:dev": "src/index.tsx", | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"types": "dist/index.d.ts", | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn clean && NODE_ENV=production node build.js && tsc", | ||
"build:js": "NODE_ENV=development node build.js", | ||
"build:typedocs": "tsx ../../../../tools/build/generate-type-docs", | ||
"clean": "rm -rf ./dist", | ||
"tsc": "tsc" | ||
}, | ||
"peerDependencies": { | ||
"@twilio-paste/anchor": "^12.1.0", | ||
"@twilio-paste/animation-library": "^2.0.0", | ||
"@twilio-paste/badge": "^8.2.0", | ||
"@twilio-paste/box": "^10.2.0", | ||
"@twilio-paste/button": "^14.1.0", | ||
"@twilio-paste/color-contrast-utils": "^5.0.0", | ||
"@twilio-paste/customization": "^8.1.1", | ||
"@twilio-paste/design-tokens": "^10.3.0", | ||
"@twilio-paste/icons": "^12.4.0", | ||
"@twilio-paste/spinner": "^14.1.2", | ||
"@twilio-paste/stack": "^8.1.0", | ||
"@twilio-paste/style-props": "^9.1.1", | ||
"@twilio-paste/styling-library": "^3.0.0", | ||
"@twilio-paste/theme": "^11.0.1", | ||
"@twilio-paste/types": "^6.0.0", | ||
"@twilio-paste/uid-library": "^2.0.0", | ||
"@twilio-paste/utils": "^5.0.0", | ||
"@types/react": "^16.8.6 || ^17.0.2 || ^18.0.27", | ||
"@types/react-dom": "^16.8.6 || ^17.0.2 || ^18.0.10", | ||
"react": "^16.8.6 || ^17.0.2 || ^18.0.0", | ||
"react-dom": "^16.8.6 || ^17.0.2 || ^18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@twilio-paste/anchor": "^12.1.0", | ||
"@twilio-paste/animation-library": "^2.0.0", | ||
"@twilio-paste/badge": "^8.2.0", | ||
"@twilio-paste/box": "^10.2.0", | ||
"@twilio-paste/button": "^14.1.0", | ||
"@twilio-paste/color-contrast-utils": "^5.0.0", | ||
"@twilio-paste/customization": "^8.1.1", | ||
"@twilio-paste/design-tokens": "^10.3.0", | ||
"@twilio-paste/icons": "^12.4.0", | ||
"@twilio-paste/spinner": "^14.1.2", | ||
"@twilio-paste/stack": "^8.1.0", | ||
"@twilio-paste/style-props": "^9.1.1", | ||
"@twilio-paste/styling-library": "^3.0.0", | ||
"@twilio-paste/theme": "^11.0.1", | ||
"@twilio-paste/types": "^6.0.0", | ||
"@twilio-paste/uid-library": "^2.0.0", | ||
"@twilio-paste/utils": "^5.0.0", | ||
"@types/react": "^18.0.27", | ||
"@types/react-dom": "^18.0.10", | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0", | ||
"tsx": "^4.0.0", | ||
"typescript": "^4.9.4" | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
packages/paste-core/components/side-panel/src/SidePanel.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { animated, useTransition } from "@twilio-paste/animation-library"; | ||
import { Box, safelySpreadBoxProps } from "@twilio-paste/box"; | ||
import type { BoxProps } from "@twilio-paste/box"; | ||
import { useMergeRefs, useWindowSize } from "@twilio-paste/utils"; | ||
import * as React from "react"; | ||
|
||
import { SidePanelContext } from "./SidePanelContext"; | ||
import type { SidePanelProps } from "./types"; | ||
|
||
const StyledSidePanelWrapper = React.forwardRef<HTMLDivElement, BoxProps>((props, ref) => ( | ||
<Box | ||
{...props} | ||
display="flex" | ||
ref={ref} | ||
position="sticky" | ||
zIndex="zIndex30" | ||
top={props.top} | ||
right={0} | ||
paddingRight={["space0", "space40", "space40"]} | ||
width={["100%", "size40", "size40"]} | ||
height={props.height} | ||
/> | ||
)); | ||
|
||
StyledSidePanelWrapper.displayName = "StyledSidePanelWrapper"; | ||
const AnimatedStyledSidePanelWrapper = animated(StyledSidePanelWrapper); | ||
|
||
const config = { | ||
mass: 0.3, | ||
tension: 288, | ||
friction: 20, | ||
}; | ||
|
||
const transitionStyles = { | ||
from: { opacity: 0, transform: "translateX(100%)" }, | ||
enter: { opacity: 1, transform: "translateX(0%)" }, | ||
leave: { opacity: 0, transform: "translateX(100%)" }, | ||
config, | ||
}; | ||
|
||
const mobileTransitionStyles = { | ||
from: { opacity: 0, transform: "translateY(100%)" }, | ||
enter: { opacity: 1, transform: "translateY(0%)" }, | ||
leave: { opacity: 0, transform: "translateY(100%)" }, | ||
config, | ||
}; | ||
|
||
const SidePanel = React.forwardRef<HTMLDivElement, SidePanelProps>( | ||
({ element = "SIDE_PANEL", label, children, ...props }, ref) => { | ||
const { sidePanelId, isOpen } = React.useContext(SidePanelContext); | ||
|
||
const { breakpointIndex } = useWindowSize(); | ||
|
||
const transitions = | ||
breakpointIndex === 0 ? useTransition(isOpen, mobileTransitionStyles) : useTransition(isOpen, transitionStyles); | ||
|
||
const screenSize = window.innerHeight; | ||
|
||
const sidePanelRef = React.useRef<HTMLDivElement>(null); | ||
const mergedSidePanelRef = useMergeRefs(sidePanelRef, ref) as React.RefObject<HTMLDivElement>; | ||
|
||
const [offsetY, setOffsetY] = React.useState(0); | ||
|
||
// Get the offset of the side panel from the top of the viewport | ||
React.useEffect(() => { | ||
const boundingClientRect = sidePanelRef?.current?.getBoundingClientRect(); | ||
setOffsetY(boundingClientRect?.y || 0); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{transitions( | ||
(styles, item) => | ||
item && ( | ||
<Box | ||
{...safelySpreadBoxProps(props)} // moved this from animated wrapper... might cause something | ||
position="absolute" | ||
role="dialog" | ||
aria-label={label} | ||
top={0} | ||
right={0} | ||
width={["100%", "auto", "auto"]} | ||
height="100%" | ||
element={element} | ||
id={sidePanelId} | ||
> | ||
<AnimatedStyledSidePanelWrapper | ||
ref={mergedSidePanelRef} | ||
element={`ANIMATED_${element}_WRAPPER`} | ||
style={styles} | ||
height={screenSize - offsetY} | ||
top={offsetY} | ||
> | ||
<Box | ||
display="flex" | ||
maxHeight="100%" | ||
flexDirection="column" | ||
width={["100%", "size40", "size40"]} | ||
borderStyle="solid" | ||
borderRadius={["borderRadius0", "borderRadius70", "borderRadius70"]} | ||
borderWidth="borderWidth10" | ||
borderColor="colorBorderWeaker" | ||
backgroundColor="colorBackgroundBody" | ||
marginTop="space40" | ||
marginBottom={["space0", "space40", "space40"]} | ||
paddingBottom="space70" | ||
overflowY="hidden" | ||
element={`INNER_${element}`} | ||
> | ||
{children} | ||
</Box> | ||
</AnimatedStyledSidePanelWrapper> | ||
</Box> | ||
), | ||
)} | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
SidePanel.displayName = "SidePanel"; | ||
|
||
export { SidePanel }; |
43 changes: 43 additions & 0 deletions
43
packages/paste-core/components/side-panel/src/SidePanelBadgeButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Badge } from "@twilio-paste/badge"; | ||
import * as React from "react"; | ||
|
||
import { SidePanelContext } from "./SidePanelContext"; | ||
import type { ButtonBadgeProps, SidePanelBadgeButtonProps } from "./types"; | ||
|
||
const ButtonBadge = React.forwardRef<HTMLButtonElement, ButtonBadgeProps>(function ButtonBadge( | ||
{ children, ...props }, | ||
ref, | ||
) { | ||
return ( | ||
<Badge {...props} as="button" ref={ref}> | ||
{children} | ||
</Badge> | ||
); | ||
}); | ||
|
||
ButtonBadge.displayName = "ButtonBadge"; | ||
|
||
const SidePanelBadgeButton = React.forwardRef<HTMLButtonElement, SidePanelBadgeButtonProps>( | ||
({ children, element = "SIDE_PANEL_BADGE", ...sidePanelButtonProps }, ref) => { | ||
const { i18nCloseSidePanelTitle, i18nOpenSidePanelTitle, isOpen, setIsOpen, sidePanelId } = | ||
React.useContext(SidePanelContext); | ||
|
||
return ( | ||
<ButtonBadge | ||
{...sidePanelButtonProps} | ||
as="button" | ||
element={element} | ||
aria-label={isOpen ? i18nCloseSidePanelTitle : i18nOpenSidePanelTitle} | ||
onClick={() => setIsOpen(!isOpen)} | ||
aria-expanded={isOpen} | ||
aria-controls={sidePanelId} | ||
ref={ref} | ||
> | ||
{children} | ||
</ButtonBadge> | ||
); | ||
}, | ||
); | ||
|
||
SidePanelBadgeButton.displayName = "SidePanelBadgeButton"; | ||
export { SidePanelBadgeButton }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TheSisb I went with
role="dialog"
but not positive that's correct