@appbuckets/react-ui
Version:
Just Another React UI Framework
122 lines (119 loc) • 3.52 kB
JavaScript
import { __rest, __read, __assign } from 'tslib';
import * as React from 'react';
import clsx from 'clsx';
import { StickyContainer, Sticky as Sticky$1 } from 'react-sticky';
import { useElementType, childrenUtils } from '@appbuckets/react-ui-core';
import { useSharedClassName } from '../utils/customHook.js';
import '../BucketTheme/BucketTheme.js';
import { useWithDefaultProps } from '../BucketTheme/BucketContext.js';
/* --------
* Component Render
* -------- */
var Sticky = function (receivedProps) {
var props = useWithDefaultProps('sticky', receivedProps);
var _a = useSharedClassName(props),
className = _a.className,
_b = _a.rest,
bottomOffset = _b.bottomOffset,
children = _b.children,
content = _b.content,
disabled = _b.disabled,
disableCompensation = _b.disableCompensation,
disableHardwareAcceleration = _b.disableHardwareAcceleration,
userDefinedOnStickyStateChange = _b.onStickyStateChange,
userDefinedStyle = _b.style,
topOffset = _b.topOffset,
rest = __rest(_b, [
'bottomOffset',
'children',
'content',
'disabled',
'disableCompensation',
'disableHardwareAcceleration',
'onStickyStateChange',
'style',
'topOffset',
]);
// ----
// Sticky State
// ----
var _c = __read(React.useState(false), 2),
isSticky = _c[0],
setIsSticky = _c[1];
// ----
// Get the Right element Type
// ----
var ElementType = useElementType(Sticky, receivedProps, props);
// ----
// Build Classes
// ----
var classes = clsx(
{ disabled: disabled },
'sticky',
isSticky && 'is-sticky',
className
);
// ----
// Internal Handlers
// ----
var handleStickyStateChange = React.useCallback(
function (nextIsSticky) {
/** If a user defined handler has been set, call it */
if (typeof userDefinedOnStickyStateChange === 'function') {
userDefinedOnStickyStateChange(nextIsSticky);
}
/** Change sticky state */
setIsSticky(nextIsSticky);
},
[userDefinedOnStickyStateChange]
);
var getTopStyle = React.useCallback(
function (top) {
/** If top is not a valid number, return empty style */
if (typeof top !== 'number') {
return {};
}
/** Adjust the next top using offset */
return {
top: top + (topOffset !== null && topOffset !== void 0 ? topOffset : 0),
};
},
[topOffset]
);
// ----
// Component Render
// ----
return React.createElement(
StickyContainer,
{ className: 'sticky-container', style: { height: '100%' } },
React.createElement(
Sticky$1,
{
bottomOffset: bottomOffset,
disableCompensation: disableCompensation,
disableHardwareAcceleration: disableHardwareAcceleration,
onStickyStateChange: handleStickyStateChange,
topOffset: (topOffset || 0) * -1,
},
function (_a) {
var style = _a.style;
/** Get the adjusted top style */
var adjustedTopStyle = getTopStyle(style.top);
/** Return the Element */
return React.createElement(
ElementType,
__assign({}, rest, {
className: classes,
style: __assign(
__assign(__assign({}, userDefinedStyle), style),
adjustedTopStyle
),
}),
childrenUtils.isNil(children) ? content : children
);
}
)
);
};
Sticky.displayName = 'Sticky';
export { Sticky as default };