1.0.1 ā€¢ Published 3 years ago

use-grid v1.0.1

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

Table of Contents

Install via npm

npm i use-grid

Quick started

git clone github.com/tseijp/use-grid
cd use-grid
npm i -D
npm start

Simple example

switch by media query

import React from 'react'
import { useMedia, useGrid } from 'use-grid'
export const App = () => {
    const isMedium      = useMedia({ minWidth:720, maxWidth:960 });
    const [ fontSize ]  = useGrid({ xs:"2em", md:"50px", xl:"75px" });
    const [ width,set ] = useGrid({ xs:8/9  , md:500   , lg:750    });
    return (
        <div style={{fontSize, width}}
            onClick={() => set((p)=>({md:p.lg,lg:p.md}))}>
            {isMedium?'šŸ˜ƒ':'šŸ˜¢'}
        </div>
    );
};

use grid system

import React from 'react'
import { useGrids } from 'use-grid'
export const App = () => {
    const face = ['šŸ™„','šŸ¤£','šŸ§','šŸ¤Æ','šŸ¤®'];
    const ref  = React.useRef(null)
    const [ws] = useGrids(face.length, (i)=>(i%2===0)
        ? { md: 1/5, xl:i/face.length/3 }
        : { md:1/10, xl:i/face.length/6 }
    , [ref]);
    return (
        <div ref={ref} style={{display:"grid", width:"95%"}}>
            {face.map( (emoji, i) =>
                <div style={{width:ws[i]}}>{emoji}</div>
            )}
        </div>
    );
};

use view system

import React from 'react';
import {useGrid, useView} from 'use-grid';
export const App = () => {
    const ref1 = React.useRef(null)
    const ref2 = React.useRef(null)
    const isView = useView(ref1)
    const [fontSize,set] = useGrid({md:100,lg:200}, [ref1])
    const [background]   = useGrid({
        none:"#000", init:"#fff",
        onView:(bool) =>
            set(bool
                ? {md:150, lg:250}
                : {md:100, lg:200})
    }, [ref1, ref2])
    return (
        <div style={{fontSize,background}}>
            <div ref={ref1}>{'šŸ˜Ž'}</div>
            {[...Array(10)].map((_,i)=>
                <div key={i}>{isView?'šŸ˜˜':'šŸ¤£'}</div>
            )}
            <div ref={ref2}>{'šŸ˜Ž'}</div>
        </div>
    )
}

Available hooks

HookDescription
useGridmake it switch value by media query with useEffect
useGridsmultiple values can be switched by media queries
useMediaget a match to media query with useEffect
useViewget a flag whether the target intersects
useLayoutGridwork like useGrid with useLayoutEffect
useLayoutGridswork like useGrids with useLayoutEffect
useLayoutMediawork like useMedia with useLayoutEffect
useLayoutViewwork like useView with useLayoutEffect

Performance Pitfalls

Grid prefix

The grid system uses containers, rows and columns to control layout and alignment.

Learn More

nameprefixmax widthmax container width
Extra smallxs<576 pxauto
Smallsm>=576 px540 px
Mediummd>=768 px720 px
Largelg>=992 px960 px
Extra largexl>=1200 px1140 px

Grid Options

nameworks
initinitial value to be specified
nonevalue when the element is not visible
onViewfunction called when the target intersects
configconfig for useGrid
viewConfigconfig for useMedia
mediaConfigconfig for useView

same works

export function Note ({children}) {
    const width1 = useGrid({sm:1, md:1/2, lg:"750px"})
    const width2 = useGrid({sm:"100%",md:"50%",lg:"750px"})
    const width3 = useGrid([["sm","100%"], ["md":"50%"], ["lg":"750px"]])
    const width4 = useGrid([[{                 maxWidth:"576px"}, "100%"],
                            [{minWidth:"576px",maxWidth:"768px"},  "50%"],
                            [{minWidth:"768px"                 },"750px"]])
    const width5 = useGrid([[                    "max-width:576px", "100%"],
                            ["min-width:576px and max-width:768px",  "50%"],
                            ["min-width:768px"                    ,"750px"]])
    const width =
       || width1
       || width2
       || width3
       || width4
       || width5
    return <div style={{width}} />
}
1.0.1

3 years ago

1.0.0

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.5

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.1

4 years ago