Last active
March 17, 2024 13:41
-
-
Save gchumillas/8ff484d74a32df26ef70dba91adf6256 to your computer and use it in GitHub Desktop.
Revisions
-
gchumillas revised this gist
Mar 17, 2024 . No changes.There are no files selected for viewing
-
gchumillas revised this gist
Mar 17, 2024 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,20 @@ import { useCallback, useMemo, useEffect } from 'react' export const useBreakpoints = <T extends Record<string, number>>(breakpoints: T): keyof T | undefined => { const searchBreakpoint = useCallback((breakpoints: { key: string, value: number }[]) => { return breakpoints.find((x) => window.innerWidth < x.value)?.key }, []) const entries = useMemo(() => { return Object .entries(breakpoints) .sort((a, b) => a[1] - b[1]) .map(([key, value]) => ({ key, value })) }, [breakpoints]) const [breakpoint, setBreakpoint] = useState(searchBreakpoint(entries)) useEffect(() => { const onResize = () => { setBreakpoint(searchBreakpoint(entries)) } -
gchumillas revised this gist
Mar 17, 2024 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
gchumillas revised this gist
Mar 17, 2024 . No changes.There are no files selected for viewing
-
gchumillas revised this gist
Mar 17, 2024 . 2 changed files with 34 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ import React from 'react' import { useBreakpoints } from './use-breakpoints.ts' // I recommend to declare breakpoints somewhere outside the component // to prevent unnecessary re-renders. // // Otherwise don't forget to wrap it around React.useMemo: // useBreakpoints(React.useMemo({ sm: 640, md: 768, ... })) const breakpoints = { sm: 640, md: 768, lg: 1024, xl: 1280 } function App() { const size = useBreakpoints(breakpoints) if (size == 'sm') { return <p>Small device.</p> } else if (size == 'md') { return <p>Medium device.</p> } else if (size == 'lg') { return <p>Large device.</p> } else if (size == 'xl') { return <p>Very large device.</p> } return ( <p>Extra large device!</p> ) } export default App This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,35 +26,3 @@ export const useBreakpoints = <T extends Record<string, number>>(breakpoints: T) return breakpoint } -
gchumillas revised this gist
Jul 14, 2023 . 1 changed file with 38 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,15 @@ import React from 'react' export const useBreakpoints = <T extends Record<string, number>>(breakpoints: T): keyof T | undefined => { const searchBreakpoint = React.useCallback((breakpoints: { key: string, value: number }[]) => { return breakpoints.find((x) => window.innerWidth < x.value)?.key }, []) const entries = React.useMemo(() => { return Object .entries(breakpoints) .sort((a, b) => a[1] - b[1]) .map(([key, value]) => ({ key, value })) }, [breakpoints]) const [breakpoint, setBreakpoint] = React.useState(searchBreakpoint(entries)) @@ -36,7 +22,39 @@ export const useBreakpoints = (breakpoints: Record<string, number>) => { return () => { window.removeEventListener('resize', onResize) } }, [entries, searchBreakpoint]) return breakpoint } // I recommend to declare breakpoints somewhere outside the component // to prevent unnecessary re-renders. // // Otherwise don't forget to wrap it around React.useMemo: // useBreakpoints(React.useMemo({ sm: 640, md: 768, ... })) const breakpoints = { sm: 640, md: 768, lg: 1024, xl: 1280 } function App() { const size = useBreakpoints(breakpoints) if (size == 'sm') { return <p>Small device.</p> } else if (size == 'md') { return <p>Medium device.</p> } else if (size == 'lg') { return <p>Large device.</p> } else if (size == 'xl') { return <p>Very large device.</p> } return ( <p>Extra large device!</p> ) } export default App -
gchumillas revised this gist
Jul 10, 2023 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,8 @@ const searchBreakpoint = (breakpoints: { key: string, value: number, index: numb // Example: // // const breakpoints = React.useMemo(() => ({ sm: 640, md: 769, lg: 1024, xl: 1280 }), []) // const breakpoint = useBreakpoints(breakpoints) // // ... later ... // -
gchumillas revised this gist
Jul 10, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ const searchBreakpoint = (breakpoints: { key: string, value: number, index: numb // if (breakpoint.index < 1) { // return <MobileVersion /> // } else if (breakpoint.index < 2) { // return <TabletVersion /> // } else { // return <DesktopVersion /> // } -
gchumillas created this gist
Jul 10, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ import React from 'react' const searchBreakpoint = (breakpoints: { key: string, value: number, index: number }[]) => { return breakpoints.find((x) => window.innerWidth < x.value) } // Example: // // const breakpoint = useBreakpoints({ sm: 640, md: 769, lg: 1024, xl: 1280 }) // // ... later ... // // if (breakpoint.index < 1) { // return <MobileVersion /> // } else if (breakpoint.index < 2) { // return <MobileVersion /> // } else { // return <DesktopVersion /> // } export const useBreakpoints = (breakpoints: Record<string, number>) => { const entries = React.useMemo(() => { return Object .entries(breakpoints) .sort((a, b) => a[1] - b[1]) .map(([key, value], index) => ({ key, value, index })) }, [breakpoints]) const [breakpoint, setBreakpoint] = React.useState(searchBreakpoint(entries)) React.useEffect(() => { const onResize = () => { setBreakpoint(searchBreakpoint(entries)) } window.addEventListener('resize', onResize) return () => { window.removeEventListener('resize', onResize) } }, [entries]) return breakpoint }