0.1.3 • Published 5 years ago

react-scrollable-appbars v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

React Scrollable Appbars

Add Material Design Appbars to your projects, following Top Appbar (https://material.io/design/components/app-bars-top.html) and Bottom Appbar (https://material.io/design/components/app-bars-bottom.html) specs.

This module integrates a Scroller component that listens to scroll changes.

Every component can show/hide based to scroll changes.

Installation

Install Material-ui packages (required)
npm install @material-ui/core @material-ui/styles

then

Install React Scrollable Appbars
npm install react-scrollable-appbars

Examples

Render a Top and a Bottom Appbar that reacts to scroll

import React, { useState } from 'react';
import { TopAppbar, BottomAppbar, Scroller } from 'react-scrollable-appbars';

import { makeStyles, useTheme } from '@material-ui/styles';
import IconButton from '@material-ui/core/IconButton';
import Icon from '@material-ui/core/Icon';
import Divider from '@material-ui/core/Divider';

const useStyles = makeStyles(theme => ({
    root: {
        display: 'flex',
        flexDirection: 'column',
        height: '100%'
    },
    row: {
        padding: theme.spacing.unit * 2
    }
}));

function App() {

    const classes = useStyles();
    const theme = useTheme();

    const [show, setShow] = useState(true);
    const [bottomReached, setBottomReached] = useState(false);

    const scrollChange = async(direction,scrollDelta) =>
        setShow((direction==='down' && scrollDelta<56) || direction==='up');

    const title = "Scrollable Appbars";
    const list = Array.from(Array(100).keys());
    
    return (
        <div className={classes.root}>
            <TopAppbar
				static
				show={show || bottomReached}
                elevation={0}
                title={title}
                color={theme.palette.primary.main}>
                <IconButton color="inherit">
                    <Icon>search</Icon>
                </IconButton>
            </TopAppbar>

            <Scroller
                paddingTop={56}
                paddingBottom={96}
				onScroll={(d,sd) => scrollChange(d,sd)}
                onBottomReached={bottom => setBottomReached(bottom)}>
                {list.map(key => <div>
                    <div className={classes.row}>Row {key}</div>
                    <Divider light/>
                </div>)}
            </Scroller>
            
            <BottomAppbar
				static
				inset
				show={show || bottomReached}
                color={theme.palette.primary.main}
                fabColor={theme.palette.secondary.main}
                fabIcon={<Icon>add</Icon>}
                fabClick={() => alert("FAB clicked")}
                menuClick={() => alert("Menu button clicked")}>
                <IconButton color="inherit">
                    <Icon>account_circle</Icon>
                </IconButton>
            </BottomAppbar>
        </div>
    );
}

export default App;

Render a Fab that reacts to scroll

import React, { useState } from 'react';
import { Fab, Scroller } from 'react-scrollable-appbars';

import { makeStyles, useTheme } from '@material-ui/styles';
import Icon from '@material-ui/core/Icon';
import Divider from '@material-ui/core/Divider';

const useStyles = makeStyles(theme => ({
    root: {
        display: 'flex',
        flexDirection: 'column',
        height: '100%'
    },
    row: {
        padding: theme.spacing.unit * 2
    }
}));

function App() {

    const classes = useStyles();
    const theme = useTheme();

    const [show, setShow] = useState(true);
    const [bottomReached, setBottomReached] = useState(false);

    const scrollChange = async(direction,scrollDelta) =>
        setShow((direction==='down' && scrollDelta<56) || direction==='up');

    const list = Array.from(Array(100).keys());
    
    return (
        <div className={classes.root}>
            <Scroller
                paddingBottom={96}
				onScroll={(d,sd) => scrollChange(d,sd)}
                onBottomReached={bottom => setBottomReached(bottom)}>
                {list.map(key => <div>
                    <div className={classes.row}>Row {key}</div>
                    <Divider light/>
                </div>)}
            </Scroller>

            <Fab
                show={show || bottomReached}
                color={theme.palette.secondary.main}
                onClick={() => alert("FAB clicked")}>
                <Icon>add</Icon>
            </Fab>
        </div>
    );
}

export default App;

Documentation

TopAppbar

PropTypeDefaultDescription
showBooleantrueShow or hide the component (with animation)
staticBooleanfalseSet the Appbar position absolute to top
colorString#ffffffBackground of the Toolbar
menuClickfunctionnullFunction to be called on menu button click
childrenComponentnullRender the icons in the right side of the Toolbar

BottomAppbar

PropTypeDefaultDescription
showBooleantrueShow or hide the component (with animation)
staticBooleanfalseSet the Appbar position absolute to bottom
insetBooleanfalseUse the inset or the default variant as described in Official docs
colorString#ffffffBackground of the Toolbar
menuClickfunctionnullFunction to be called on menu button click
fabColorString#ffffffBackground of the Fab
fabIconComponentnullRender the icon of the Fab
fabClickfunctionnullFunction to be called on Fab click
childrenComponentnullRender the icons in the right side of the Toolbar

Fab

PropTypeDefaultDescription
showBooleantrueShow or hide the component (with animation)
colorString#ffffffBackground of the Fab
onClickfunctionnullFunction to be called on Fab click
childrenComponentnullRender the icon

Changelog

v0.1.3

  • Refactored with new React Hooks
  • NEW Added Fab component
  • NEW Set default theme for components
  • FIX Adjusted behavior of BottomAppbar in large screens
  • FIX Scroll performance optimization

v0.1.2

v0.1.0

  • Initial release

License

MIT

0.1.3

5 years ago

0.1.2-b

5 years ago

0.1.2

5 years ago

0.1.0-d

5 years ago

0.1.0-c

5 years ago

0.1.0-b

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago