@workday/canvas-kit-preview-react
Version:
Canvas Kit Preview is made up of components that have the full design and a11y review, are part of the DS ecosystem and are approved for use in product. The API's could be subject to change, but not without strong communication and migration strategies.
44 lines (43 loc) • 2.26 kB
JavaScript
/** @jsxRuntime classic */
/** @jsx jsx */
import * as React from 'react';
import { createComponent, useIsRTL } from '@workday/canvas-kit-react/common';
import { css, jsx } from '@emotion/react';
import { TertiaryButton } from '@workday/canvas-kit-react/button';
import { space } from '@workday/canvas-kit-react/tokens';
import { transformationImportIcon } from '@workday/canvas-system-icons-web';
import { Tooltip } from '@workday/canvas-kit-react/tooltip';
import { SidePanelContext } from './hooks';
/**
* A toggle button styled specifically for the side panel container.
*/
export const SidePanelToggleButton = createComponent('button')({
displayName: 'SidePanel.ToggleButton',
Component({ variant = undefined, icon = transformationImportIcon, tooltipTextExpand = 'Expand', tooltipTextCollapse = 'Collapse', ...elemProps }) {
const context = React.useContext(SidePanelContext);
const useRTLOrigin = () => {
const isRTL = useIsRTL();
// if the direction is set to RTl, flip the origin
if (isRTL) {
return context.origin === 'left' ? 'right' : 'left';
}
// Otherwise, default to returning the origin
return context.origin;
};
const rtlOrigin = useRTLOrigin();
// Note: Depending on the collapsed width, the button could "jump" to it's final position.
const buttonStyle = css({
position: 'absolute',
top: space.m,
width: space.l,
right: context.state === 'collapsed' ? 0 : rtlOrigin === 'left' ? space.s : undefined,
left: context.state === 'collapsed' ? 0 : rtlOrigin === 'right' ? space.s : undefined,
margin: context.state === 'collapsed' ? 'auto' : 0,
transform: context.state === 'collapsed' || context.state === 'collapsing'
? `scaleX(${rtlOrigin === 'left' ? '1' : '-1'})`
: `scaleX(${rtlOrigin === 'left' ? '-1' : '1'})`,
});
return (jsx(Tooltip, { title: context.state === 'collapsed' ? tooltipTextExpand : tooltipTextCollapse, type: "muted" },
jsx(TertiaryButton, { type: "button", css: buttonStyle, icon: icon, variant: variant, ...elemProps })));
},
});