@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.
98 lines (97 loc) • 3.27 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.useSidePanel = exports.SidePanelContext = void 0;
const React = __importStar(require("react"));
exports.SidePanelContext = React.createContext({
state: 'expanded',
origin: 'left',
});
const common_1 = require("@workday/canvas-kit-react/common");
/**
*
* This hook manages the state and `aria-` attributes for the SidePanel. It takes an optional
* configuration object:
*
* ```tsx
* import {useSidePanel} from '@workday/canvas-kit-preview-react/side-panel';
*
* const {expanded, setExpanded, panelProps, labelProps, controlProps} = useSidePanel({
* initialExpanded: false,
* panelId: 'custom-panel-id',
* labelId: 'custom-label-id',
* });
* ```
*/
const useSidePanel = (config) => {
const [touched, setTouched] = React.useState(false);
const configParams = config
? config
: {
initialExpanded: true,
panelId: undefined,
labelId: undefined,
};
const { initialExpanded = true, panelId: panelIdParam, labelId: labelIdParam } = configParams;
const [expanded, setExpanded] = React.useState(initialExpanded);
const panelId = (0, common_1.useUniqueId)(panelIdParam);
const labelId = (0, common_1.useUniqueId)(labelIdParam);
// This prevents keyframes animations from rendering prematurely
const handleSetTouched = () => {
if (touched === false) {
setTouched(true);
}
};
const handleClick = () => {
handleSetExpanded(!expanded);
};
const handleSetExpanded = (newExpandedState) => {
handleSetTouched();
setExpanded(newExpandedState);
};
const panelProps = {
expanded: expanded,
id: panelId,
'aria-labelledby': labelId,
touched,
};
const labelProps = {
id: labelId,
};
const controlProps = {
'aria-controls': panelId,
'aria-expanded': expanded,
'aria-labelledby': labelId,
onClick: handleClick,
};
return {
expanded,
setExpanded: handleSetExpanded,
panelProps,
labelProps,
controlProps,
};
};
exports.useSidePanel = useSidePanel;
;