|
| 1 | + |
| 2 | +# React Resize Detector Context |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +A lightweight React context that leverages [react-resize-detector](https://github.com/maslianok/react-resize-detector) to detect the current breakpoint based on an element's width. It provides utility functions and helper components to conditionally render content based on responsive breakpoints. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Dynamically detects the current breakpoint based on element width. |
| 13 | +- Supports custom breakpoint names (e.g., "XS", "SM", "MD", "LG", "XL" or any arbitrary names like "Smart", "Mini", etc.). |
| 14 | +- Utility functions: |
| 15 | + - `isAtLeast(breakpoint)`: Returns `true` if the current breakpoint is greater than or equal to the provided one. |
| 16 | + - `isBelow(breakpoint)`: Returns `true` if the current breakpoint is less than the provided one. |
| 17 | + - `valueByBreakpoint(mapping)`: Returns a value from the provided mapping based on the current breakpoint. |
| 18 | +- Helper component `BreakpointConditional` for conditional rendering. |
| 19 | +- Error logging for invalid configurations (e.g. duplicate values, width not set, etc.). |
| 20 | +- Fully typed in TypeScript with comprehensive IDE support. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Requirements |
| 25 | + |
| 26 | +- **Node.js**: >= 18.0.0 |
| 27 | +- **React**: >= 17 |
| 28 | +- **React-DOM**: >= 17 |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +Install via npm: |
| 35 | + |
| 36 | +``` |
| 37 | +npm install my-breakpoint-package |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### Basic Usage |
| 45 | + |
| 46 | +Wrap your component tree with the `BreakpointProvider` and provide a breakpoint configuration. Then, access the current breakpoint and utility functions via the `useBreakpoint` hook. |
| 47 | + |
| 48 | +```typescript |
| 49 | +import React from 'react'; |
| 50 | +import { BreakpointProvider, useBreakpoint } from 'my-breakpoint-package'; |
| 51 | + |
| 52 | +const breakpoints = { |
| 53 | + XS: 0, |
| 54 | + SM: 500, |
| 55 | + MD: 700, |
| 56 | + LG: 900, |
| 57 | + XL: 1100, |
| 58 | +}; |
| 59 | + |
| 60 | +const ResponsiveComponent = () => { |
| 61 | + const { width, breakpoint, isAtLeast, valueByBreakpoint } = useBreakpoint(); |
| 62 | + return ( |
| 63 | + <div> |
| 64 | + <p>Current width: {width}px</p> |
| 65 | + <p>Current breakpoint: {breakpoint}</p> |
| 66 | + <p>Is at least MD: {isAtLeast('MD') ? '✅' : '❌'}</p> |
| 67 | + <p> |
| 68 | + Mapping: {valueByBreakpoint({ |
| 69 | + XS: 'Extra Small', |
| 70 | + SM: 'Small', |
| 71 | + MD: 'Medium', |
| 72 | + LG: 'Large', |
| 73 | + XL: 'Extra Large' |
| 74 | + })} |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +const App = () => ( |
| 81 | + <BreakpointProvider breakpoints={breakpoints}> |
| 82 | + <ResponsiveComponent /> |
| 83 | + </BreakpointProvider> |
| 84 | +); |
| 85 | + |
| 86 | +export default App; |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +### Conditional Rendering with BreakpointConditional |
| 92 | + |
| 93 | +Use `BreakpointConditional` to render content only when certain breakpoint conditions are met. |
| 94 | + |
| 95 | +```typescript |
| 96 | +import React from 'react'; |
| 97 | +import { BreakpointProvider, BreakpointConditional } from 'my-breakpoint-package'; |
| 98 | + |
| 99 | +const breakpoints = { |
| 100 | + XS: 0, |
| 101 | + SM: 500, |
| 102 | + MD: 700, |
| 103 | + LG: 900, |
| 104 | + XL: 1100, |
| 105 | +}; |
| 106 | + |
| 107 | +const ConditionalComponent = () => ( |
| 108 | + <div> |
| 109 | + <BreakpointConditional isAtLeast="MD"> |
| 110 | + <p>😊 This content is visible from MD and up.</p> |
| 111 | + </BreakpointConditional> |
| 112 | + <BreakpointConditional isBelow="LG"> |
| 113 | + <p>🚀 This content is visible for breakpoints below LG.</p> |
| 114 | + </BreakpointConditional> |
| 115 | + </div> |
| 116 | +); |
| 117 | + |
| 118 | +const App = () => ( |
| 119 | + <BreakpointProvider breakpoints={breakpoints}> |
| 120 | + <ConditionalComponent /> |
| 121 | + </BreakpointProvider> |
| 122 | +); |
| 123 | + |
| 124 | +export default App; |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### Custom Breakpoints Example |
| 130 | + |
| 131 | +You can define your own custom breakpoints with any names. For instance, using car sizes: |
| 132 | + |
| 133 | +```typescript |
| 134 | +import React from 'react'; |
| 135 | +import { BreakpointProvider, useBreakpoint } from 'my-breakpoint-package'; |
| 136 | + |
| 137 | +const carBreakpoints = { |
| 138 | + Smart: 0, |
| 139 | + Mini: 350, |
| 140 | + Sedan: 600, |
| 141 | + SUV: 900, |
| 142 | + Ram: 1200, |
| 143 | +}; |
| 144 | + |
| 145 | +const CarComponent = () => { |
| 146 | + const { width, breakpoint, valueByBreakpoint } = useBreakpoint(); |
| 147 | + return ( |
| 148 | + <div> |
| 149 | + <p>Current width: {width}px</p> |
| 150 | + <p>Current car size: {breakpoint}</p> |
| 151 | + <p> |
| 152 | + Mapping: {valueByBreakpoint({ |
| 153 | + Smart: '🚗 Smart', |
| 154 | + Mini: '🚙 Mini', |
| 155 | + Sedan: '🚘 Sedan', |
| 156 | + SUV: '🚐 SUV', |
| 157 | + Ram: '🚚 Ram' |
| 158 | + })} |
| 159 | + </p> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +}; |
| 163 | + |
| 164 | +const App = () => ( |
| 165 | + <BreakpointProvider breakpoints={carBreakpoints}> |
| 166 | + <CarComponent /> |
| 167 | + </BreakpointProvider> |
| 168 | +); |
| 169 | + |
| 170 | +export default App; |
| 171 | +``` |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## Available Scripts |
| 176 | + |
| 177 | +You can use the following scripts from your command line: |
| 178 | + |
| 179 | +- **`npm run build`** |
| 180 | + Builds the package (using [tsup](https://github.com/egoist/tsup)). |
| 181 | + |
| 182 | +- **`npm run dev`** |
| 183 | + Runs development mode concurrently with build watch, Storybook, and tests. |
| 184 | + |
| 185 | +- **`npm run storybook`** |
| 186 | + Launches Storybook for interactive component demos on port 6006. |
| 187 | + |
| 188 | +- **`npm run test`** |
| 189 | + Runs tests using Vitest. |
| 190 | + |
| 191 | +- **`npm run test:ci`** |
| 192 | + Runs tests with coverage. |
| 193 | + |
| 194 | +- **`npm run lint`** |
| 195 | + Runs biome check with auto-fix. |
| 196 | + |
| 197 | +- **`npm run release`** |
| 198 | + Builds and triggers the release process. |
| 199 | + |
| 200 | +--- |
| 201 | + |
| 202 | +## License |
| 203 | + |
| 204 | +This project is licensed under the MIT License. |
| 205 | + |
| 206 | +--- |
0 commit comments