UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

32 lines (31 loc) 1.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useWindowSize = void 0; const react_1 = __importDefault(require("react")); const useMount_1 = require("./useMount"); const getFromWindow = (property, defaultValue) => { var _a; if (typeof window !== undefined) { return (_a = window[property]) !== null && _a !== void 0 ? _a : defaultValue; } return defaultValue; }; const useWindowSize = () => { const [width, setWidth] = react_1.default.useState(getFromWindow('innerWidth', 0)); const [height, setHeight] = react_1.default.useState(getFromWindow('innerHeight', 0)); const onResize = () => { setWidth(window.innerWidth); setHeight(window.innerHeight); }; (0, useMount_1.useMount)(() => { window.addEventListener('resize', onResize); return () => { window.removeEventListener('resize', onResize); }; }); return { width, height }; }; exports.useWindowSize = useWindowSize;