@@ -176,7 +184,8 @@ export const ExampleWithCallback = () => {
const [ref, setRef] = useState(null)
const [background, setBg] = useState('red')
-useBreakpoint(ref, {
+useBreakpoint({
+containerEl: ref,
breakpoints: {
sm: 320,
md: 480,
@@ -215,7 +224,8 @@ const Example = () => {
const ref = useRef(null)
const [background, setBg] = useState('red')
- useBreakpoint(ref, {
+ useBreakpoint({
+ containerEl: ref,
breakpoints: {
sm: 320,
md: 480,
@@ -230,7 +240,7 @@ const Example = () => {
return (
<>
-
+
The background will change when container's size is changed
{
The `useBreakpoint` takes the following parameters:
-#### `containerEl`
-
-- Type: `HTMLElement`
-
#### `Options`
- Type: `UseBreakpointOpts`
## API
+#### `containerEl`
+
+- Type: `HTMLElement`
+- Default: `window.document.body`
+
#### `currentBreakpoint`
- Type: `string`
diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md
index 00127a0..3b78862 100644
--- a/packages/components/CHANGELOG.md
+++ b/packages/components/CHANGELOG.md
@@ -1,5 +1,12 @@
# @react-awesome/components
+## 1.0.18
+
+### Patch Changes
+
+- Updated dependencies
+ - @react-awesome/use-breakpoint@1.0.0
+
## 1.0.17
### Patch Changes
diff --git a/packages/components/package.json b/packages/components/package.json
index b17d8ed..da7a5af 100644
--- a/packages/components/package.json
+++ b/packages/components/package.json
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/components",
- "version": "1.0.17",
+ "version": "1.0.18",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
@@ -45,6 +45,6 @@
"@react-awesome/use-selection-range": "0.0.3",
"@react-awesome/use-previous": "0.0.3",
"@react-awesome/use-toggle": "0.0.1",
- "@react-awesome/use-breakpoint": "0.0.2"
+ "@react-awesome/use-breakpoint": "1.0.0"
}
}
diff --git a/packages/use-breakpoint/CHANGELOG.md b/packages/use-breakpoint/CHANGELOG.md
index 7efce45..d58a0ab 100644
--- a/packages/use-breakpoint/CHANGELOG.md
+++ b/packages/use-breakpoint/CHANGELOG.md
@@ -1,5 +1,11 @@
# @react-awesome/use-breakpoint
+## 1.0.0
+
+### Major Changes
+
+- fix warning in next.js
+
## 0.0.2
### Patch Changes
diff --git a/packages/use-breakpoint/package.json b/packages/use-breakpoint/package.json
index 9ec719d..1403a8e 100644
--- a/packages/use-breakpoint/package.json
+++ b/packages/use-breakpoint/package.json
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/use-breakpoint",
- "version": "0.0.2",
+ "version": "1.0.0",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
diff --git a/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.spec.tsx b/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.spec.tsx
index deadebd..8f8a208 100644
--- a/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.spec.tsx
+++ b/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.spec.tsx
@@ -24,13 +24,21 @@ describe('useBreakpoint', () => {
})
it('Should do nothing when element is null', async () => {
- const { unmount } = renderHook(() => useBreakpoint(null))
+ const { unmount } = renderHook(() =>
+ useBreakpoint({
+ containerEl: null,
+ }),
+ )
expect(MockObserverInstance.observe).not.toHaveBeenCalled()
})
it('Should observe resize event on passed element', async () => {
- const { unmount } = renderHook(() => useBreakpoint(mockEl))
+ const { unmount } = renderHook(() =>
+ useBreakpoint({
+ containerEl: mockEl,
+ }),
+ )
expect(MockObserverInstance.observe).toHaveBeenCalled()
@@ -43,7 +51,8 @@ describe('useBreakpoint', () => {
try {
console.error = vitest.fn()
renderHook(() =>
- useBreakpoint(mockEl, {
+ useBreakpoint({
+ containerEl: mockEl,
breakpoints: {
sm: 1,
md: 1,
@@ -74,7 +83,8 @@ describe('useBreakpoint', () => {
})
renderHook(() =>
- useBreakpoint(mockEl, {
+ useBreakpoint({
+ containerEl: mockEl,
callbacks: {
sm: mockCallback,
},
@@ -99,7 +109,8 @@ describe('useBreakpoint', () => {
})
renderHook(() =>
- useBreakpoint(mockEl, {
+ useBreakpoint({
+ containerEl: mockEl,
callbacks: {
lg: mockCallback,
},
@@ -124,7 +135,8 @@ describe('useBreakpoint', () => {
})
renderHook(() =>
- useBreakpoint(mockEl, {
+ useBreakpoint({
+ containerEl: mockEl,
callbacks: {
'2xl': mockCallback,
},
diff --git a/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.tsx b/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.tsx
index 8c050d9..967b1a0 100644
--- a/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.tsx
+++ b/packages/use-breakpoint/src/useBreakpoint/useBreakpoint.tsx
@@ -60,20 +60,22 @@ export type UseBreakpointOpts<
callbacks?: Partial>
fallbackValue?: keyof B
useResizeObserver?: boolean
+ containerEl?: HTMLElement | null
}
export function useBreakpoint>(
- containerEl: HTMLElement | null,
opts: UseBreakpointOpts = {},
): { currentBreakpoint?: keyof B } & UseBreakpointUtils {
const {
breakpoints: BPS = DEFAULT_BREAKPOINTS,
callbacks,
fallbackValue,
+ containerEl,
} = opts
const [currentBreakpoint, setCurrentBreakpoint] = useState<
keyof typeof BPS | undefined
>(fallbackValue as keyof typeof BPS)
+ const [el, setEl] = useState(null)
const BPS_VALUES_ARR = useMemo(
() => Object.values(BPS).sort((a, b) => a - b),
@@ -117,7 +119,12 @@ export function useBreakpoint>(
)
useEffect(() => {
- if (!containerEl) return
+ if (typeof containerEl === 'undefined') setEl(window.document.body)
+ else if (containerEl !== el) setEl(containerEl)
+ }, [containerEl, el])
+
+ useEffect(() => {
+ if (!el) return
const callback = (entries: ResizeObserverEntry[]) => {
const [entry] = entries
@@ -132,20 +139,20 @@ export function useBreakpoint>(
const resizeObserver = new ResizeObserver(callback)
- resizeObserver.observe(containerEl)
+ resizeObserver.observe(el)
return () => {
resizeObserver.disconnect()
}
- }, [callbacks, containerEl, determineCurrentBreakpoint])
+ }, [callbacks, determineCurrentBreakpoint, el])
// @ts-expect-error
return {
currentBreakpoint,
- smaller: smaller(BPS, containerEl),
- smallerOrEqual: smallerOrEqual(BPS, containerEl),
- greater: greater(BPS, containerEl),
- greaterOrEqual: greaterOrEqual(BPS, containerEl),
- between: between(BPS, containerEl),
+ smaller: smaller(BPS, el),
+ smallerOrEqual: smallerOrEqual(BPS, el),
+ greater: greater(BPS, el),
+ greaterOrEqual: greaterOrEqual(BPS, el),
+ between: between(BPS, el),
}
}