UNPKG

react95-native

Version:

Refreshed Windows 95 style UI components for your React Native app

87 lines (76 loc) 3.15 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import React from 'react'; import { StyleSheet, View, ScrollView as RNScrollView } from 'react-native'; import { ChevronIcon } from '../..'; const chevronIconSize = 20; // TODO: performance improvements (callbacks, refs ...etc) const ScrollView = ({ children, scrollViewProps = {}, style, ...rest }) => { const [contentOffset, setContentOffset] = React.useState(0); const [contentSize, setContentSize] = React.useState(0); const [scrollViewSize, setScrollViewSize] = React.useState(0); // TODO: that's a naive approach. if it causes problems // trigger a callback when last child becomes fully visible const lastElementVisible = contentOffset + scrollViewSize < contentSize - chevronIconSize; const handleScroll = e => { var _scrollViewProps$onSc; (_scrollViewProps$onSc = scrollViewProps.onScroll) === null || _scrollViewProps$onSc === void 0 ? void 0 : _scrollViewProps$onSc.call(scrollViewProps, e); setContentOffset(e.nativeEvent.contentOffset.x); }; const handleContentSizeChange = (width, height) => { var _scrollViewProps$onCo; (_scrollViewProps$onCo = scrollViewProps.onContentSizeChange) === null || _scrollViewProps$onCo === void 0 ? void 0 : _scrollViewProps$onCo.call(scrollViewProps, width, height); setContentSize(width); }; const handleLayout = e => { var _scrollViewProps$onLa; (_scrollViewProps$onLa = scrollViewProps.onLayout) === null || _scrollViewProps$onLa === void 0 ? void 0 : _scrollViewProps$onLa.call(scrollViewProps, e); setScrollViewSize(e.nativeEvent.layout.width); }; return /*#__PURE__*/React.createElement(View, _extends({ style: [styles.wrapper, { flexDirection: 'row' }, style] }, rest), /*#__PURE__*/React.createElement(View, { style: [styles.content] }, /*#__PURE__*/React.createElement(RNScrollView, _extends({}, scrollViewProps, { showsHorizontalScrollIndicator: false, scrollEventThrottle: 100, onScroll: handleScroll, onContentSizeChange: handleContentSizeChange, onLayout: handleLayout, horizontal: true }), children)), /*#__PURE__*/React.createElement(View, { style: styles.chevronIcon }, lastElementVisible && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ChevronIcon, { segments: 3, direction: "right", style: { marginRight: 1 } }), /*#__PURE__*/React.createElement(ChevronIcon, { segments: 3, direction: "right" })))); }; const styles = StyleSheet.create({ wrapper: { display: 'flex', position: 'relative' }, content: { flexGrow: 1, flexShrink: 1 }, chevronIcon: { justifyContent: 'center', flexDirection: 'row', alignSelf: 'flex-start', width: chevronIconSize } }); export default ScrollView; //# sourceMappingURL=Toolbar.js.map