UNPKG

@workday/canvas-kit-labs-react

Version:

Canvas Kit Labs is an incubator for new and experimental components. Since we have a rather rigorous process for getting components in at a production level, it can be valuable to make them available earlier while we continuously iterate on the API/functi

135 lines (134 loc) 5.46 kB
"use strict"; 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.useSidePanelModel = void 0; const common_1 = require("@workday/canvas-kit-react/common"); const React = __importStar(require("react")); exports.useSidePanelModel = (0, common_1.createModelHook)({ defaultConfig: { /** * The initial transition state of the SidePanel. * @default 'expanded' */ initialTransitionState: 'expanded', /** * Which side the SidePanel originates from. Uses logical properties for RTL support. * - `start`: Left side in LTR, right side in RTL * - `end`: Right side in LTR, left side in RTL * @default 'start' */ origin: 'start', /** * The unique ID for the panel element. If not provided, a unique ID will be generated. * This ID is used for the `aria-controls` attribute on the toggle button. */ panelId: '', /** * The unique ID for the panel's label element. If not provided, a unique ID will be generated. * This ID is used for the `aria-labelledby` attribute on both the panel and toggle button. */ labelId: '', /** * Callback fired when the SidePanel's transition state changes. * Use this to react to state changes including animation states. * @param state The new transition state */ onStateTransition(state) { // no-op by default }, }, })(config => { const panelId = (0, common_1.useUniqueId)(config.panelId); const labelId = (0, common_1.useUniqueId)(config.labelId); const [transitionState, setTransitionStateInternal] = React.useState(config.initialTransitionState); // Wrap setTransitionState to call the onStateTransition callback const setTransitionState = React.useCallback((newState) => { setTransitionStateInternal(newState); config.onStateTransition(newState); }, [config]); const state = { ...config, /** * The unique ID for the panel element. Used for `aria-controls` on the toggle button. */ panelId, /** * The unique ID for the panel's label element. Used for `aria-labelledby` on the panel * and toggle button to provide an accessible name. */ labelId, /** * The current transition state of the SidePanel. This tracks both the expanded/collapsed * state and the animation states (expanding/collapsing). */ transitionState, }; const events = { /** * Expand the SidePanel. This sets the state directly to `expanded` without animation. * For animated expansion, use the toggle button which triggers `setExpandingState`. */ expand() { setTransitionState('expanded'); }, /** * Collapse the SidePanel. This sets the state directly to `collapsed` without animation. * For animated collapse, use the toggle button which triggers `setCollapsingState`. */ collapse() { setTransitionState('collapsed'); }, /** * Handler for the CSS transition end event. This should be called when the width * transition completes to finalize the state from `expanding` to `expanded` or * from `collapsing` to `collapsed`. */ handleAnimationEnd(event) { if (event.propertyName === 'width') { if (transitionState === 'expanding') { setTransitionState('expanded'); } else if (transitionState === 'collapsing') { setTransitionState('collapsed'); } } return event; }, /** * Triggers the start of a transition animation. This toggles between expanding * and collapsing states based on the current state. */ handleAnimationStart() { if (transitionState === 'collapsed' || transitionState === 'collapsing') { setTransitionState('expanding'); } else if (transitionState === 'expanded' || transitionState === 'expanding') { setTransitionState('collapsing'); } return undefined; }, }; return { state, events }; });