@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
100 lines (99 loc) • 4.12 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.PopupModelContext = exports.usePopupModel = void 0;
const React = __importStar(require("react"));
const common_1 = require("@workday/canvas-kit-react/common");
const disclosure_1 = require("@workday/canvas-kit-react/disclosure");
// eslint-disable-next-line no-empty-function
const noop = () => { };
exports.usePopupModel = (0, common_1.createModelHook)({
// create enough of a model to use `Popup.Card` without a `Popup` container component.
defaultContext: { state: {}, events: { show: noop, hide: noop } },
defaultConfig: {
...disclosure_1.useDisclosureModel.defaultConfig,
/**
* Optional reference to an element that should receive focus when a popup is hidden. If left
* blank, focus will return to the `targetRef`
*/
returnFocusRef: undefined,
/**
* Optional reference to an element that should receive focus when a popup is shown. If left blank,
* focus will be moved to the first focusable element inside the popup.
*/
initialFocusRef: undefined,
},
requiredConfig: disclosure_1.useDisclosureModel.requiredConfig,
})(config => {
const stackRef = React.useRef(null);
const targetRef = React.useRef(null);
const [placement, setPlacement] = React.useState('bottom');
const disclosure = (0, disclosure_1.useDisclosureModel)(config);
const state = {
...disclosure.state,
/**
* Reference of the stack item used in the PopupStack. This value will be used in all
* Popup-related behaviors.
*/
stackRef,
/**
* Reference of the target element. This will point to the element that triggered the showing of a
* Popup and is used in Popup-related behaviors.
*/
targetRef,
/**
* Optional reference to an element that should receive focus when a popup is shown. If left blank,
* focus will be moved to the first focusable element inside the popup.
*/
initialFocusRef: config.initialFocusRef,
/**
* Optional reference to an element that should receive focus when a popup is hidden. If left
* blank, focus will return to the `targetRef`
*/
returnFocusRef: config.returnFocusRef,
/**
* The placement chosen by the positioning Popper. This should get set prior to showing the popup
* content.
* @default 'bottom'
*/
placement,
};
const events = {
...disclosure.events,
/**
* Called whenever a popup placement changes around a target. This is useful for animations that
* depend on the placement chosen.
*/
updatePlacement(data) {
setPlacement(data.placement);
},
};
return {
state,
events,
};
});
// create enough of a model to use `Popup.Card` without a `Popup` container component.
exports.PopupModelContext = exports.usePopupModel.Context;
;