@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.
141 lines (140 loc) • 5.98 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SidePanel = void 0;
/** @jsxRuntime classic */
/** @jsx jsx */
const React = __importStar(require("react"));
const common_1 = require("@workday/canvas-kit-react/common");
const react_1 = require("@emotion/react");
const tokens_1 = require("@workday/canvas-kit-react/tokens");
const hooks_1 = require("./hooks");
const SidePanelToggleButton_1 = require("./SidePanelToggleButton");
const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
const createKeyframes = (from, to) => {
const normalized = {
from: typeof from === 'number' ? from + 'px' : from,
to: typeof to === 'number' ? to + 'px' : to,
};
return (0, react_1.keyframes) `
from {
width: ${normalized.from};
min-width: ${normalized.from};
max-width: ${normalized.from};
} to {
width: ${normalized.to};
min-width: ${normalized.to};
max-width: ${normalized.to};
}
`;
};
const containerVariantStyle = {
alternate: {
backgroundColor: tokens_1.colors.frenchVanilla100,
...tokens_1.depth[5],
},
standard: {
backgroundColor: tokens_1.colors.soap100,
},
};
const Panel = (0, common_1.styled)('section')({
overflow: 'hidden',
position: 'relative',
boxSizing: 'border-box',
height: '100%',
outline: `${(0, canvas_kit_styling_1.px2rem)(1)} solid transparent`,
});
exports.SidePanel = (0, common_1.createComponent)('section')({
displayName: 'SidePanel',
Component({ children, collapsedWidth = 64, expanded = true, expandedWidth = 320, onAnimationEnd, onAnimationStart, onExpandedChange, onStateTransition, origin = 'left', variant = 'standard', touched, ...elemProps }, ref, Element) {
const [state, setState] = React.useState(expanded ? 'expanded' : 'collapsed');
React.useEffect(() => {
if (typeof onExpandedChange !== 'undefined') {
onExpandedChange(expanded);
}
}, [expanded, onExpandedChange]);
React.useEffect(() => {
if (typeof onStateTransition !== 'undefined') {
onStateTransition(state);
}
}, [state, onStateTransition]);
const motion = {
collapse: createKeyframes(expandedWidth, collapsedWidth),
expand: createKeyframes(collapsedWidth, expandedWidth),
};
const handleAnimationEnd = (event) => {
if (event.currentTarget === event.target) {
if (event.animationName === motion.collapse.name) {
setState('collapsed');
}
if (event.animationName === motion.expand.name) {
setState('expanded');
}
}
if (typeof onAnimationEnd !== 'undefined') {
onAnimationEnd(event);
}
};
const handleAnimationStart = (event) => {
if (event.currentTarget === event.target) {
if (event.animationName === motion.collapse.name ||
event.animationName === motion.expand.name) {
setState(expanded ? 'expanding' : 'collapsing');
}
}
if (typeof onAnimationStart !== 'undefined') {
onAnimationStart(event);
}
};
return ((0, react_1.jsx)(Panel, { ref: ref, as: Element, css: [
{
width: expanded ? expandedWidth : collapsedWidth,
maxWidth: expanded ? expandedWidth : collapsedWidth,
// mounted.current will be false on the first render, thus you won't get an unwanted animation here
// Will animate again if you force a re-render (like in Storybook)
animation: touched
? `${expanded ? motion.expand : motion.collapse} 200ms ease-out`
: undefined,
},
containerVariantStyle[variant],
], onAnimationEnd: handleAnimationEnd, onAnimationStart: handleAnimationStart, ...elemProps },
(0, react_1.jsx)(hooks_1.SidePanelContext.Provider, { value: {
state,
origin,
} }, children)));
},
subComponents: {
/**
* `SidePanel.ToggleButton` is a control that is meant to toggle between `expanded = true` and
* `expanded = false` states. It must be used within the `SidePanel` component as a child. Use
* in conjunction with `useSidePanel`'s `controlProps`, otherwise it does not come with explicit
* `onClick` handlers.
*
* For accessibility purposes, it must be the first focusable element. We recommend that you
* keep it as the first child of `SidePanel`
*/
ToggleButton: SidePanelToggleButton_1.SidePanelToggleButton,
},
});
;