|
@@ -11,16 +11,56 @@ import { withUnstatedContainers } from './UnstatedUtils';
|
|
|
const logger = loggerFactory('growi:cli:StickyStretchableScroller');
|
|
const logger = loggerFactory('growi:cli:StickyStretchableScroller');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * USAGE:
|
|
|
|
|
+ *
|
|
|
|
|
+ const calcViewHeight = useCallback(() => {
|
|
|
|
|
+ const containerElem = document.querySelector('#sticky-elem');
|
|
|
|
|
+ const containerTop = containerElem.getBoundingClientRect().top;
|
|
|
|
|
+
|
|
|
|
|
+ // stretch to the bottom of window
|
|
|
|
|
+ return window.innerHeight - containerTop;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <StickyStretchableScroller
|
|
|
|
|
+ contentsElemSelector="#long-contents-elem"
|
|
|
|
|
+ stickyElemSelector="#sticky-elem"
|
|
|
|
|
+ calcViewHeightFunc={calcViewHeight}
|
|
|
|
|
+ >
|
|
|
|
|
+ <div id="scroll-elem">
|
|
|
|
|
+ ...
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </StickyStretchableScroller>
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ or
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <StickyStretchableScroller
|
|
|
|
|
+ scrollTargetId="scroll-elem"
|
|
|
|
|
+ contentsElemSelector="#long-contents-elem"
|
|
|
|
|
+ stickyElemSelector="#sticky-elem"
|
|
|
|
|
+ calcViewHeightFunc={calcViewHeight}
|
|
|
|
|
+ />
|
|
|
|
|
+ );
|
|
|
|
|
+ */
|
|
|
const StickyStretchableScroller = (props) => {
|
|
const StickyStretchableScroller = (props) => {
|
|
|
|
|
|
|
|
|
|
+ let { scrollTargetSelector } = props;
|
|
|
const {
|
|
const {
|
|
|
navigationContainer,
|
|
navigationContainer,
|
|
|
children, contentsElemSelector, stickyElemSelector,
|
|
children, contentsElemSelector, stickyElemSelector,
|
|
|
calcViewHeightFunc, calcContentsHeightFunc,
|
|
calcViewHeightFunc, calcContentsHeightFunc,
|
|
|
} = props;
|
|
} = props;
|
|
|
|
|
|
|
|
- const id = children.props.id;
|
|
|
|
|
|
|
+ if (scrollTargetSelector == null && children == null) {
|
|
|
|
|
+ throw new Error('Either of scrollTargetSelector or children is required');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ if (scrollTargetSelector == null) {
|
|
|
|
|
+ scrollTargetSelector = `#${children.props.id}`;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Reset scrollbar
|
|
* Reset scrollbar
|
|
@@ -38,16 +78,16 @@ const StickyStretchableScroller = (props) => {
|
|
|
? calcContentsHeightFunc(contentsElem)
|
|
? calcContentsHeightFunc(contentsElem)
|
|
|
: contentsElem.getBoundingClientRect().height;
|
|
: contentsElem.getBoundingClientRect().height;
|
|
|
|
|
|
|
|
- logger.debug('viewHeight', viewHeight);
|
|
|
|
|
- logger.debug('contentsHeight', contentsHeight);
|
|
|
|
|
|
|
+ logger.debug(`[${scrollTargetSelector}] viewHeight`, viewHeight);
|
|
|
|
|
+ logger.debug(`[${scrollTargetSelector}] contentsHeight`, contentsHeight);
|
|
|
|
|
|
|
|
- $(`#${id}`).slimScroll({
|
|
|
|
|
|
|
+ $(scrollTargetSelector).slimScroll({
|
|
|
railVisible: true,
|
|
railVisible: true,
|
|
|
position: 'right',
|
|
position: 'right',
|
|
|
height: viewHeight,
|
|
height: viewHeight,
|
|
|
});
|
|
});
|
|
|
if (contentsHeight < viewHeight) {
|
|
if (contentsHeight < viewHeight) {
|
|
|
- $(`#${id}`).slimScroll({ destroy: true });
|
|
|
|
|
|
|
+ $(scrollTargetSelector).slimScroll({ destroy: true });
|
|
|
}
|
|
}
|
|
|
}, [contentsElemSelector, calcViewHeightFunc, calcContentsHeightFunc]);
|
|
}, [contentsElemSelector, calcViewHeightFunc, calcContentsHeightFunc]);
|
|
|
|
|
|
|
@@ -106,9 +146,10 @@ const StickyStretchableScroller = (props) => {
|
|
|
|
|
|
|
|
StickyStretchableScroller.propTypes = {
|
|
StickyStretchableScroller.propTypes = {
|
|
|
navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
|
|
navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
|
|
|
- children: PropTypes.node.isRequired,
|
|
|
|
|
contentsElemSelector: PropTypes.string.isRequired,
|
|
contentsElemSelector: PropTypes.string.isRequired,
|
|
|
|
|
|
|
|
|
|
+ children: PropTypes.node,
|
|
|
|
|
+ scrollTargetSelector: PropTypes.string,
|
|
|
stickyElemSelector: PropTypes.string,
|
|
stickyElemSelector: PropTypes.string,
|
|
|
|
|
|
|
|
calcViewHeightFunc: PropTypes.func,
|
|
calcViewHeightFunc: PropTypes.func,
|