@amaui/ui-react
Version:
UI for React
147 lines (139 loc) • 6.13 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["ids", "offset", "offsetStart", "addClassName", "addStyle", "onActive", "parent", "className", "style", "children"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { is, unique, isEnvironment } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import { staticClassName } from '../utils';
const useStyle = styleMethod(theme => ({
root: {}
}), {
name: 'amaui-SpyScroll'
});
const SpyScroll = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiSpyScroll?.props?.default), props_), [props_]);
const {
ids,
offset,
offsetStart,
addClassName,
addStyle,
onActive,
parent,
className,
style,
children
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const refs = {
root: React.useRef(undefined),
active: React.useRef([]),
parent: React.useRef(parent),
props: React.useRef(undefined),
onActive: React.useRef(onActive)
};
refs.props.current = props;
refs.parent.current = parent;
refs.onActive.current = onActive;
React.useEffect(() => {
// Listen to window scroll value
const isVisible = element => new Promise(resolve => {
if (!(element && refs.root.current)) return resolve(null);
const observer = new IntersectionObserver(entries => {
const intersecting = entries?.[0].isIntersecting;
// clean up
observer.disconnect();
return resolve(intersecting);
}, {
root: refs.parent.current
});
observer.observe(element);
});
const method = async () => {
// Find first active id
let id;
const rootElement_ = isEnvironment('browser') ? refs.parent.current || refs.root.current?.ownerDocument || window.document : undefined;
for (const item of refs.props.current.ids || []) {
id = await isVisible(rootElement_.querySelector(`#${item}`.replace('##', '#')));
if (id) {
id = item;
break;
}
}
if (id && refs.root.current) {
// Update all elements in root
// Remove active className and style from
// all previous active elements
for (const element of refs.active.current) {
// addClassName
element.classList.remove(refs.props.current.addClassName);
// addStyle
if (refs.props.current.addStyle) {
Object.keys(refs.props.current.addStyle).forEach(item => {
element.style.removeProperty(item);
});
}
}
// And add to all elements with href containing #id with addClassName and style
refs.active.current = unique([
// a elemnts with id
...Array.from(refs.root.current.querySelectorAll(`[href$='#${id}']`)),
// elements active for multiple ids
...Array.from(refs.root.current.querySelectorAll(`[data-amaui-spy-scroll~=${id}]`))]).filter(Boolean);
for (const element of refs.active.current) {
// addClassName
if (!element.classList.contains(refs.props.current.addClassName)) element.classList.add(refs.props.current.addClassName);
// addStyle
if (refs.props.current.addStyle) {
Object.keys(refs.props.current.addStyle).forEach(item => {
element.style[item] = refs.props.current.addStyle[item];
});
}
}
// onActive
if (is('function', refs.onActive.current)) refs.onActive.current(id);
} else {
// Remove active className and style from
// all previous active elements
for (const element of refs.active.current) {
// addClassName
element.classList.remove(refs.props.current.addClassName);
// addStyle
if (refs.props.current.addStyle) {
Object.keys(refs.props.current.addStyle).forEach(item => {
element.style.removeProperty(item);
});
}
}
}
};
// Initial
method();
const rootElement = isEnvironment('browser') ? refs.parent.current || refs.root.current?.ownerDocument || window.document : undefined;
if (refs.root.current) {
rootElement.addEventListener('scroll', method);
}
return () => {
if (refs.root.current) {
rootElement.removeEventListener('scroll', method);
}
};
}, [parent]);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.cloneElement(children, _objectSpread({
ref: item => {
if (ref) {
if (is('function', ref)) ref(item);else ref.current = item;
}
refs.root.current = item;
},
className: classNames([staticClassName('SpyScroll', theme) && ['amaui-SpyScroll-root'], children.props.className, className, classes.root])
}, other)));
});
SpyScroll.displayName = 'amaui-SpyScroll';
export default SpyScroll;