@trackunit/react-components 0.1.414 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +76 -0
- package/index.esm.js +78 -3
- package/package.json +1 -1
- package/src/hooks/index.d.ts +1 -0
- package/src/hooks/useViewportSize.d.ts +32 -0
package/index.cjs.js
CHANGED
|
@@ -1292,6 +1292,81 @@ const useSelfUpdatingRef = (initialState) => {
|
|
|
1292
1292
|
return stateRef;
|
|
1293
1293
|
};
|
|
1294
1294
|
|
|
1295
|
+
/**
|
|
1296
|
+
* Maps viewport size keys to their corresponding state property names.
|
|
1297
|
+
*/
|
|
1298
|
+
const propsMap = {
|
|
1299
|
+
xs: "isXs",
|
|
1300
|
+
sm: "isSm",
|
|
1301
|
+
md: "isMd",
|
|
1302
|
+
lg: "isLg",
|
|
1303
|
+
xl: "isXl",
|
|
1304
|
+
"2xl": "is2xl",
|
|
1305
|
+
"3xl": "is3xl",
|
|
1306
|
+
};
|
|
1307
|
+
/**
|
|
1308
|
+
* The default state for viewport sizes, with all values set to false.
|
|
1309
|
+
*/
|
|
1310
|
+
const defaultState = {
|
|
1311
|
+
isXs: false,
|
|
1312
|
+
isSm: false,
|
|
1313
|
+
isMd: false,
|
|
1314
|
+
isLg: false,
|
|
1315
|
+
isXl: false,
|
|
1316
|
+
is2xl: false,
|
|
1317
|
+
is3xl: false,
|
|
1318
|
+
};
|
|
1319
|
+
/**
|
|
1320
|
+
* A custom React hook that provides real-time information about the current viewport size.
|
|
1321
|
+
*
|
|
1322
|
+
* This hook listens to changes in the viewport size and returns an object with boolean values
|
|
1323
|
+
* indicating which breakpoints are currently active. It's useful for creating responsive
|
|
1324
|
+
* layouts and components that need to adapt to different screen sizes.
|
|
1325
|
+
*
|
|
1326
|
+
* @returns {ViewportSizeState} An object containing boolean values for each viewport size breakpoint.
|
|
1327
|
+
* @example
|
|
1328
|
+
* function MyComponent() {
|
|
1329
|
+
* const viewportSize = useViewportSize();
|
|
1330
|
+
*
|
|
1331
|
+
* if (viewportSize.isLg) {
|
|
1332
|
+
* return <LargeScreenLayout />;
|
|
1333
|
+
* } else if (viewportSize.isMd) {
|
|
1334
|
+
* return <MediumScreenLayout />;
|
|
1335
|
+
* } else {
|
|
1336
|
+
* return <SmallScreenLayout />;
|
|
1337
|
+
* }
|
|
1338
|
+
* }
|
|
1339
|
+
*/
|
|
1340
|
+
const useViewportSize = () => {
|
|
1341
|
+
const [viewportSize, setViewportSize] = React.useState(() => defaultState);
|
|
1342
|
+
const updateViewportSize = React.useCallback(() => {
|
|
1343
|
+
const newViewportSize = sharedUtils.objectEntries(uiDesignTokens.themeScreenSizeAsNumber).reduce((acc, [size, minWidth]) => {
|
|
1344
|
+
const matches = window.matchMedia(`(min-width: ${minWidth}px)`).matches;
|
|
1345
|
+
return {
|
|
1346
|
+
...acc,
|
|
1347
|
+
[propsMap[size]]: matches,
|
|
1348
|
+
};
|
|
1349
|
+
}, Object.assign({}, defaultState));
|
|
1350
|
+
setViewportSize(newViewportSize);
|
|
1351
|
+
}, []);
|
|
1352
|
+
React.useEffect(() => {
|
|
1353
|
+
// Initial check
|
|
1354
|
+
updateViewportSize();
|
|
1355
|
+
// Set up listeners for each breakpoint
|
|
1356
|
+
const mediaQueryLists = sharedUtils.objectEntries(uiDesignTokens.themeScreenSizeAsNumber).map(([_, minWidth]) => window.matchMedia(`(min-width: ${minWidth}px)`));
|
|
1357
|
+
mediaQueryLists.forEach(mql => {
|
|
1358
|
+
mql.addEventListener("change", updateViewportSize);
|
|
1359
|
+
});
|
|
1360
|
+
// Cleanup
|
|
1361
|
+
return () => {
|
|
1362
|
+
mediaQueryLists.forEach(mql => {
|
|
1363
|
+
mql.removeEventListener("change", updateViewportSize);
|
|
1364
|
+
});
|
|
1365
|
+
};
|
|
1366
|
+
}, [updateViewportSize]);
|
|
1367
|
+
return viewportSize;
|
|
1368
|
+
};
|
|
1369
|
+
|
|
1295
1370
|
const hasFocus = () => typeof document !== "undefined" && document.hasFocus();
|
|
1296
1371
|
/**
|
|
1297
1372
|
* Use this hook to disable functionality while the tab is hidden within the browser or to react to focus or blur events
|
|
@@ -4990,4 +5065,5 @@ exports.usePrompt = usePrompt;
|
|
|
4990
5065
|
exports.useResize = useResize;
|
|
4991
5066
|
exports.useSelfUpdatingRef = useSelfUpdatingRef;
|
|
4992
5067
|
exports.useTimeout = useTimeout;
|
|
5068
|
+
exports.useViewportSize = useViewportSize;
|
|
4993
5069
|
exports.useWindowActivity = useWindowActivity;
|
package/index.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import React__default, { useRef, useMemo, useEffect, useState, useReducer, useCallback, memo, forwardRef, createContext, useContext, isValidElement, cloneElement, Children } from 'react';
|
|
4
|
-
import { objectKeys, objectValues } from '@trackunit/shared-utils';
|
|
5
|
-
import { rentalStatusPalette, intentPalette, generalPalette, criticalityPalette, activityPalette, utilizationPalette, sitesPalette, color } from '@trackunit/ui-design-tokens';
|
|
4
|
+
import { objectKeys, objectEntries, objectValues } from '@trackunit/shared-utils';
|
|
5
|
+
import { rentalStatusPalette, intentPalette, generalPalette, criticalityPalette, activityPalette, utilizationPalette, sitesPalette, themeScreenSizeAsNumber, color } from '@trackunit/ui-design-tokens';
|
|
6
6
|
import { iconNames } from '@trackunit/ui-icons';
|
|
7
7
|
import IconSpriteMini from '@trackunit/ui-icons/icons-sprite-mini.svg';
|
|
8
8
|
import IconSpriteOutline from '@trackunit/ui-icons/icons-sprite-outline.svg';
|
|
@@ -1272,6 +1272,81 @@ const useSelfUpdatingRef = (initialState) => {
|
|
|
1272
1272
|
return stateRef;
|
|
1273
1273
|
};
|
|
1274
1274
|
|
|
1275
|
+
/**
|
|
1276
|
+
* Maps viewport size keys to their corresponding state property names.
|
|
1277
|
+
*/
|
|
1278
|
+
const propsMap = {
|
|
1279
|
+
xs: "isXs",
|
|
1280
|
+
sm: "isSm",
|
|
1281
|
+
md: "isMd",
|
|
1282
|
+
lg: "isLg",
|
|
1283
|
+
xl: "isXl",
|
|
1284
|
+
"2xl": "is2xl",
|
|
1285
|
+
"3xl": "is3xl",
|
|
1286
|
+
};
|
|
1287
|
+
/**
|
|
1288
|
+
* The default state for viewport sizes, with all values set to false.
|
|
1289
|
+
*/
|
|
1290
|
+
const defaultState = {
|
|
1291
|
+
isXs: false,
|
|
1292
|
+
isSm: false,
|
|
1293
|
+
isMd: false,
|
|
1294
|
+
isLg: false,
|
|
1295
|
+
isXl: false,
|
|
1296
|
+
is2xl: false,
|
|
1297
|
+
is3xl: false,
|
|
1298
|
+
};
|
|
1299
|
+
/**
|
|
1300
|
+
* A custom React hook that provides real-time information about the current viewport size.
|
|
1301
|
+
*
|
|
1302
|
+
* This hook listens to changes in the viewport size and returns an object with boolean values
|
|
1303
|
+
* indicating which breakpoints are currently active. It's useful for creating responsive
|
|
1304
|
+
* layouts and components that need to adapt to different screen sizes.
|
|
1305
|
+
*
|
|
1306
|
+
* @returns {ViewportSizeState} An object containing boolean values for each viewport size breakpoint.
|
|
1307
|
+
* @example
|
|
1308
|
+
* function MyComponent() {
|
|
1309
|
+
* const viewportSize = useViewportSize();
|
|
1310
|
+
*
|
|
1311
|
+
* if (viewportSize.isLg) {
|
|
1312
|
+
* return <LargeScreenLayout />;
|
|
1313
|
+
* } else if (viewportSize.isMd) {
|
|
1314
|
+
* return <MediumScreenLayout />;
|
|
1315
|
+
* } else {
|
|
1316
|
+
* return <SmallScreenLayout />;
|
|
1317
|
+
* }
|
|
1318
|
+
* }
|
|
1319
|
+
*/
|
|
1320
|
+
const useViewportSize = () => {
|
|
1321
|
+
const [viewportSize, setViewportSize] = useState(() => defaultState);
|
|
1322
|
+
const updateViewportSize = useCallback(() => {
|
|
1323
|
+
const newViewportSize = objectEntries(themeScreenSizeAsNumber).reduce((acc, [size, minWidth]) => {
|
|
1324
|
+
const matches = window.matchMedia(`(min-width: ${minWidth}px)`).matches;
|
|
1325
|
+
return {
|
|
1326
|
+
...acc,
|
|
1327
|
+
[propsMap[size]]: matches,
|
|
1328
|
+
};
|
|
1329
|
+
}, Object.assign({}, defaultState));
|
|
1330
|
+
setViewportSize(newViewportSize);
|
|
1331
|
+
}, []);
|
|
1332
|
+
useEffect(() => {
|
|
1333
|
+
// Initial check
|
|
1334
|
+
updateViewportSize();
|
|
1335
|
+
// Set up listeners for each breakpoint
|
|
1336
|
+
const mediaQueryLists = objectEntries(themeScreenSizeAsNumber).map(([_, minWidth]) => window.matchMedia(`(min-width: ${minWidth}px)`));
|
|
1337
|
+
mediaQueryLists.forEach(mql => {
|
|
1338
|
+
mql.addEventListener("change", updateViewportSize);
|
|
1339
|
+
});
|
|
1340
|
+
// Cleanup
|
|
1341
|
+
return () => {
|
|
1342
|
+
mediaQueryLists.forEach(mql => {
|
|
1343
|
+
mql.removeEventListener("change", updateViewportSize);
|
|
1344
|
+
});
|
|
1345
|
+
};
|
|
1346
|
+
}, [updateViewportSize]);
|
|
1347
|
+
return viewportSize;
|
|
1348
|
+
};
|
|
1349
|
+
|
|
1275
1350
|
const hasFocus = () => typeof document !== "undefined" && document.hasFocus();
|
|
1276
1351
|
/**
|
|
1277
1352
|
* Use this hook to disable functionality while the tab is hidden within the browser or to react to focus or blur events
|
|
@@ -4872,4 +4947,4 @@ const cvaClickable = cvaMerge([
|
|
|
4872
4947
|
},
|
|
4873
4948
|
});
|
|
4874
4949
|
|
|
4875
|
-
export { Alert, Badge, Breadcrumb, BreadcrumbContainer, BreadcrumbItem, Button, Card, CardBody, CardFooter, CardHeader, Collapse, CompletionStatusIndicator, CopyableText, Drawer, EmptyState, EmptyValue, ExternalLink, Heading, Icon, IconButton, Indicator, KPICard, MenuItem, MenuList, MoreMenu, Notice, PackageNameStoryComponent, Page, PageContent, PageHeader, Pagination, Polygon, Popover, PopoverContent, PopoverTitle, PopoverTrigger, Prompt, ROLE_CARD, SectionHeader, Sidebar, SkeletonLines, Spacer, Spinner, StarButton, Tab, TabContent, TabList, Tabs, Tag, Text, Timeline, TimelineElement, ToggleGroup, Tooltip, ValueBar, VirtualizedList, WidgetBody, cvaButton, cvaButtonPrefixSuffix, cvaButtonSpinner, cvaButtonSpinnerContainer, cvaClickable, cvaIconButton, cvaIndicator, cvaIndicatorIcon, cvaIndicatorIconBackground, cvaIndicatorLabel, cvaIndicatorPing, cvaInteractableItem, cvaMenuItem, cvaMenuItemLabel, cvaMenuItemPrefix, cvaMenuItemStyle, cvaMenuItemSuffix, docs, getDevicePixelRatio, getValueBarColorByValue, iconColorNames, iconPalette, setLocalStorage, useClickOutside, useContinuousTimeout, useDebounce, useDevicePixelRatio, useGeometry, useHover, useIsFirstRender, useIsFullscreen, useIsTextCutOff, useLocalStorage, useLocalStorageReducer, useOptionallyElevatedState, useOverflowItems, usePopoverContext, usePrompt, useResize, useSelfUpdatingRef, useTimeout, useWindowActivity };
|
|
4950
|
+
export { Alert, Badge, Breadcrumb, BreadcrumbContainer, BreadcrumbItem, Button, Card, CardBody, CardFooter, CardHeader, Collapse, CompletionStatusIndicator, CopyableText, Drawer, EmptyState, EmptyValue, ExternalLink, Heading, Icon, IconButton, Indicator, KPICard, MenuItem, MenuList, MoreMenu, Notice, PackageNameStoryComponent, Page, PageContent, PageHeader, Pagination, Polygon, Popover, PopoverContent, PopoverTitle, PopoverTrigger, Prompt, ROLE_CARD, SectionHeader, Sidebar, SkeletonLines, Spacer, Spinner, StarButton, Tab, TabContent, TabList, Tabs, Tag, Text, Timeline, TimelineElement, ToggleGroup, Tooltip, ValueBar, VirtualizedList, WidgetBody, cvaButton, cvaButtonPrefixSuffix, cvaButtonSpinner, cvaButtonSpinnerContainer, cvaClickable, cvaIconButton, cvaIndicator, cvaIndicatorIcon, cvaIndicatorIconBackground, cvaIndicatorLabel, cvaIndicatorPing, cvaInteractableItem, cvaMenuItem, cvaMenuItemLabel, cvaMenuItemPrefix, cvaMenuItemStyle, cvaMenuItemSuffix, docs, getDevicePixelRatio, getValueBarColorByValue, iconColorNames, iconPalette, setLocalStorage, useClickOutside, useContinuousTimeout, useDebounce, useDevicePixelRatio, useGeometry, useHover, useIsFirstRender, useIsFullscreen, useIsTextCutOff, useLocalStorage, useLocalStorageReducer, useOptionallyElevatedState, useOverflowItems, usePopoverContext, usePrompt, useResize, useSelfUpdatingRef, useTimeout, useViewportSize, useWindowActivity };
|
package/package.json
CHANGED
package/src/hooks/index.d.ts
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { themeScreenSizeAsNumber } from "@trackunit/ui-design-tokens";
|
|
2
|
+
type ViewportSize = keyof typeof themeScreenSizeAsNumber;
|
|
3
|
+
/**
|
|
4
|
+
* Represents the state of viewport sizes as boolean values.
|
|
5
|
+
* Each property is true if the viewport width is greater than or equal to the corresponding breakpoint.
|
|
6
|
+
*/
|
|
7
|
+
type ViewportSizeState = {
|
|
8
|
+
[K in ViewportSize as `is${Capitalize<K>}`]: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A custom React hook that provides real-time information about the current viewport size.
|
|
12
|
+
*
|
|
13
|
+
* This hook listens to changes in the viewport size and returns an object with boolean values
|
|
14
|
+
* indicating which breakpoints are currently active. It's useful for creating responsive
|
|
15
|
+
* layouts and components that need to adapt to different screen sizes.
|
|
16
|
+
*
|
|
17
|
+
* @returns {ViewportSizeState} An object containing boolean values for each viewport size breakpoint.
|
|
18
|
+
* @example
|
|
19
|
+
* function MyComponent() {
|
|
20
|
+
* const viewportSize = useViewportSize();
|
|
21
|
+
*
|
|
22
|
+
* if (viewportSize.isLg) {
|
|
23
|
+
* return <LargeScreenLayout />;
|
|
24
|
+
* } else if (viewportSize.isMd) {
|
|
25
|
+
* return <MediumScreenLayout />;
|
|
26
|
+
* } else {
|
|
27
|
+
* return <SmallScreenLayout />;
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
export declare const useViewportSize: () => ViewportSizeState;
|
|
32
|
+
export {};
|