项目作者: vitaliemaldur

项目描述 :
React hook for responsive design logic
高级语言: JavaScript
项目地址: git://github.com/vitaliemaldur/react-breakpoints-hook.git
创建时间: 2020-01-07T10:49:38Z
项目社区:https://github.com/vitaliemaldur/react-breakpoints-hook

开源协议:

下载


react-breakpoints-hook

Installation

  1. yarn add react-breakpoints-hook

useBreakpoints configuration

  1. // Configuration object consists of breakpoints names which have associated a min and a max value in pixels.
  2. // Note: if max value is omitted it will be considered as the window width.
  3. {
  4. breakpointName1: {min: 0, max: 360},
  5. breakpointName2: {min: 361, max: 960},
  6. breakpointName3: {min: 961, max: 1400},
  7. breakpointName4: {min: 1401, max: null},
  8. breakpointName5: {min: 500, max: 1300},
  9. }

useBreakpoints usage

  1. // returns an object with corresponding boolean flags for each breakpoint, gets updated at rezise
  2. let { xs, sm, md, lg } = useBreakpoints({
  3. xs: {min: 0, max: 360},
  4. sm: {min: 361, max: 960},
  5. md: {min: 961, max: 1400},
  6. lg: {min: 1401, max: null},
  7. });

useCurrentWidth usage

  1. // returns current width, gets updated at rezise
  2. let width = useCurrentWidth();
  3. });

Example

  1. import React from 'react';
  2. import { useBreakpoints, useCurrentWidth } from 'react-breakpoints-hook';
  3. const App = () => {
  4. let width = useCurrentWidth();
  5. let { xs, sm, md, lg } = useBreakpoints({
  6. xs: {min: 0, max: 360},
  7. sm: {min: 361, max: 960},
  8. md: {min: 961, max: 1400},
  9. lg: {min: 1401, max: null},
  10. });
  11. return (
  12. <div>
  13. <h1>
  14. {`Current width -> ${width}`}
  15. </h1>
  16. <p>
  17. {`Breakpoint: xs(${xs}) sm(${sm}) md(${md}) lg(${lg})`}
  18. </p>
  19. </div>
  20. );
  21. }
  22. export default App;