-
Notifications
You must be signed in to change notification settings - Fork 119
feat(side-panel): add useSidePanelState hook, animation zhoosh #4230
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5bf6cf4
chore(side-panel): add zhoosh
nkrantz b4ba31b
docs(side-panel): more examples
nkrantz a6e467a
chore: things i forgot
nkrantz c5ff4e5
chore: update state return and only use modal on mobile
nkrantz 861c23f
chore: save some lines
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": minor | ||
"@twilio-paste/core": minor | ||
--- | ||
|
||
[Side Panel] Update mobile styles, add useSidePanelState hook, animation fixes |
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] new export from Side Panel: useSidePanelState() |
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
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 |
---|---|---|
@@ -1,12 +1,41 @@ | ||
import { animated, useTransition } from "@twilio-paste/animation-library"; | ||
import { Box, safelySpreadBoxProps } from "@twilio-paste/box"; | ||
import { Box, getCustomElementStyles, safelySpreadBoxProps } from "@twilio-paste/box"; | ||
import type { BoxProps } from "@twilio-paste/box"; | ||
import { ModalDialogPrimitiveContent, ModalDialogPrimitiveOverlay } from "@twilio-paste/modal-dialog-primitive"; | ||
import { css, styled } from "@twilio-paste/styling-library"; | ||
import { pasteBaseStyles, useTheme } from "@twilio-paste/theme"; | ||
import { useMergeRefs, useWindowSize } from "@twilio-paste/utils"; | ||
import * as React from "react"; | ||
|
||
import { SidePanelContext } from "./SidePanelContext"; | ||
import type { SidePanelProps } from "./types"; | ||
|
||
const SidePanelMobileOverlay = animated( | ||
// @ts-expect-error the styled div color prop from emotion is clashing with our color style prop in BoxProps | ||
styled(ModalDialogPrimitiveOverlay)( | ||
css({ | ||
backgroundColor: "colorBackgroundOverlay", | ||
position: "fixed", | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
width: "100%", | ||
zIndex: "zIndex80", | ||
}), | ||
/* | ||
* import Paste Theme Based Styles due to portal positioning. | ||
* reach portal is a sibling to the main app, so you are now | ||
* no longer a child of the theme provider. We need to re-set | ||
* some of the base styles that we rely on inheriting from | ||
* such as font-family and line-height so that compositions | ||
* of paste components in the side panel are styled correctly. | ||
*/ | ||
pasteBaseStyles, | ||
krisantrobus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getCustomElementStyles, | ||
), | ||
); | ||
|
||
const StyledSidePanelWrapper = React.forwardRef<HTMLDivElement, BoxProps>((props, ref) => ( | ||
<Box | ||
{...props} | ||
|
@@ -19,6 +48,7 @@ const StyledSidePanelWrapper = React.forwardRef<HTMLDivElement, BoxProps>((props | |
paddingRight={["space0", "space40", "space40"]} | ||
width={["100%", "size40", "size40"]} | ||
height={props.height} | ||
boxSizing="content-box" | ||
/> | ||
)); | ||
|
||
|
@@ -31,87 +61,153 @@ const config = { | |
friction: 20, | ||
}; | ||
|
||
const transitionStyles = { | ||
from: { opacity: 0, transform: "translateX(100%)" }, | ||
enter: { opacity: 1, transform: "translateX(0%)" }, | ||
leave: { opacity: 0, transform: "translateX(100%)" }, | ||
config, | ||
}; | ||
interface SidePanelContentsProps extends SidePanelProps { | ||
sidePanelId: string; | ||
styles: any; | ||
isMobile: boolean; | ||
} | ||
|
||
const mobileTransitionStyles = { | ||
from: { opacity: 0, transform: "translateY(100%)" }, | ||
enter: { opacity: 1, transform: "translateY(0%)" }, | ||
leave: { opacity: 0, transform: "translateY(100%)" }, | ||
config, | ||
const getAs = (isMobile: boolean): any => { | ||
if (isMobile) return ModalDialogPrimitiveContent as any; | ||
return "div"; | ||
}; | ||
|
||
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 SidePanelContents = React.forwardRef<HTMLDivElement, SidePanelContentsProps>( | ||
({ label, element, sidePanelId, styles, isMobile, children, ...props }, ref) => { | ||
// Get the offset of the side panel from the top of the viewport | ||
const sidePanelRef = React.useRef<HTMLDivElement>(null); | ||
const mergedSidePanelRef = useMergeRefs(sidePanelRef, ref) as React.RefObject<HTMLDivElement>; | ||
|
||
const screenSize = window.innerHeight; | ||
PixeledCode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 ( | ||
<Box | ||
{...safelySpreadBoxProps(props)} | ||
position="absolute" | ||
role="dialog" | ||
as={getAs(isMobile)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: i feel like this would just be a ternary and save some lines There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, just pushed this |
||
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={["100%", screenSize - offsetY, screenSize - offsetY]} | ||
top={[0, offsetY, offsetY]} | ||
position="relative" | ||
overflow="hidden" | ||
> | ||
<Box | ||
display="flex" | ||
flexDirection="column" | ||
width={["100%", "388px", "388px"]} // 400px - 12px | ||
position="absolute" | ||
top={0} | ||
left={[0, 12, 12]} | ||
bottom={0} | ||
borderStyle="solid" | ||
borderBottomLeftRadius={["borderRadius0", "borderRadius70", "borderRadius70"]} | ||
borderBottomRightRadius={["borderRadius0", "borderRadius70", "borderRadius70"]} | ||
borderTopRightRadius={["borderRadius60", "borderRadius70", "borderRadius70"]} | ||
borderTopLeftRadius={["borderRadius60", "borderRadius70", "borderRadius70"]} | ||
borderWidth="borderWidth10" | ||
borderColor="colorBorderWeaker" | ||
backgroundColor="colorBackgroundBody" | ||
marginTop={["space100", "space40", "space40"]} | ||
marginBottom={["space0", "space40", "space40"]} | ||
paddingBottom="space70" | ||
element={`INNER_${element}`} | ||
> | ||
{children} | ||
</Box> | ||
</AnimatedStyledSidePanelWrapper> | ||
</Box> | ||
); | ||
}, | ||
); | ||
SidePanelContents.displayName = "SidePanelContents"; | ||
|
||
const SidePanel = React.forwardRef<HTMLDivElement, SidePanelProps>( | ||
({ element = "SIDE_PANEL", label, children, ...props }, ref) => { | ||
const theme = useTheme(); | ||
const { sidePanelId, isOpen } = React.useContext(SidePanelContext); | ||
// Determine whether this is the initial render in order to block enter animations | ||
const [isFirstRender, setIsFirstRender] = React.useState(true); | ||
React.useEffect(() => { | ||
if (isFirstRender) { | ||
setIsFirstRender(false); | ||
} | ||
}, [isFirstRender]); | ||
|
||
// Define transition styles for both breakpoints | ||
const transitionStyles = { | ||
from: isFirstRender ? undefined : { opacity: 0, width: "0px" }, | ||
enter: { opacity: 1, width: "400px" }, | ||
leave: { opacity: 0, width: "0px" }, | ||
config, | ||
}; | ||
const mobileTransitionStyles = { | ||
from: isFirstRender ? undefined : { opacity: 0, transform: "translateY(100%)" }, | ||
enter: { opacity: 1, transform: "translateY(0%)" }, | ||
leave: { opacity: 0, transform: "translateY(100%)" }, | ||
config, | ||
}; | ||
|
||
// Set mobile or desktop transitions based on breakpointIndex | ||
const { breakpointIndex } = useWindowSize(); | ||
const desktopTransitions = useTransition(isOpen, transitionStyles); | ||
const mobileTransitions = useTransition(isOpen, mobileTransitionStyles); | ||
const transitions = React.useMemo(() => { | ||
if (breakpointIndex === 0) return mobileTransitions; | ||
return desktopTransitions; | ||
}, [breakpointIndex, desktopTransitions, mobileTransitions]); | ||
|
||
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} | ||
item && | ||
(breakpointIndex === 0 ? ( | ||
<SidePanelMobileOverlay | ||
theme={theme} | ||
data-paste-element={`${element}_OVERLAY`} | ||
style={{ opacity: styles.opacity }} | ||
> | ||
<AnimatedStyledSidePanelWrapper | ||
ref={mergedSidePanelRef} | ||
element={`ANIMATED_${element}_WRAPPER`} | ||
style={styles} | ||
height={screenSize - offsetY} | ||
top={offsetY} | ||
<SidePanelContents | ||
{...props} | ||
element={element} | ||
sidePanelId={sidePanelId} | ||
styles={styles} | ||
label={label} | ||
isMobile | ||
ref={ref} | ||
> | ||
<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> | ||
), | ||
{children} | ||
</SidePanelContents> | ||
</SidePanelMobileOverlay> | ||
) : ( | ||
<SidePanelContents | ||
{...props} | ||
element={element} | ||
sidePanelId={sidePanelId} | ||
styles={styles} | ||
label={label} | ||
isMobile={false} | ||
ref={ref} | ||
> | ||
{children} | ||
</SidePanelContents> | ||
)), | ||
)} | ||
</> | ||
); | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as React from "react"; | ||
|
||
import type { SidePanelStateReturn, UseSidePanelStateProps } from "./types"; | ||
|
||
export const useSidePanelState = ({ open = false }: UseSidePanelStateProps = {}): SidePanelStateReturn => { | ||
const [isOpen, setIsOpen] = React.useState(open); | ||
|
||
return { | ||
isOpen, | ||
setIsOpen, | ||
}; | ||
}; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.