Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "useCurrentBreakpoints" hook #318

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/hooks/UseCurrentBreakpoints.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import { useCurrentBreakpoints } from 'atomic-layout'

const UseCurrentBreakpointsScenario = () => {
const breakpoints = useCurrentBreakpoints()

return (
<p>
Current breakpoint:{' '}
<span data-test-id="current-breakpoints">{breakpoints.join()}</span>
</p>
)
}

export default UseCurrentBreakpointsScenario
29 changes: 29 additions & 0 deletions examples/hooks/useCurrentBreakpoints.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('useCurrentBreakpoints', () => {
before(() => {
cy.loadStory(['hooks'], ['usecurrentbreakpoints'])
})

it('Returns the default breakpoint on initial render', () => {
cy.get('[data-test-id="current-breakpoints"]').should('have.text', 'xs')
})

it('Returns "sm" breakpoint name on "sm" breakpoint', () => {
cy.setBreakpoint('sm')
cy.get('[data-test-id="current-breakpoints"]').should('have.text', 'sm')
})

it('Returns "md" breakpoint name on "md" breakpoint', () => {
cy.setBreakpoint('md')
cy.get('[data-test-id="current-breakpoints"]').should('have.text', 'md')
})

it('Returns "lg" breakpoint name on "lg" breakpoint', () => {
cy.setBreakpoint('lg')
cy.get('[data-test-id="current-breakpoints"]').should('have.text', 'lg')
})

it('Returns "xl" breakpoint name on "xl" breakpoint', () => {
cy.setBreakpoint('xl')
cy.get('[data-test-id="current-breakpoints"]').should('have.text', 'xl')
})
})
2 changes: 2 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ storiesOf('Components|Visible', module).add(
import UseViewportChange from './hooks/UseViewportChange'
import UseResponsiveValue from './hooks/UseResponsiveValue'
import UseBreakpointChange from './hooks/UseBreakpointChange'
import UseCurrentBreakpoints from './hooks/UseCurrentBreakpoints'
import UseResponsiveProps from './hooks/UseResponsiveProps'

storiesOf('Hooks', module)
.add('useViewportChange', () => <UseViewportChange />)
.add('useResponsiveValue', () => <UseResponsiveValue />)
.add('useBreakpointChange', () => <UseBreakpointChange />)
.add('useCurrentBreakpoints', () => <UseCurrentBreakpoints />)
.add('useResponsiveProps', () => <UseResponsiveProps />)

/**
Expand Down
1 change: 1 addition & 0 deletions packages/atomic-layout-emotion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
useMediaQuery,
useViewportChange,
useBreakpointChange,
useCurrentBreakpoints,
useResponsiveValue,
useResponsiveProps,
useResponsiveQuery,
Expand Down
33 changes: 33 additions & 0 deletions packages/atomic-layout/src/hooks/useCurrentBreakpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState } from 'react'
import useViewportChange from './useViewportChange'
import { Layout, createMediaQuery } from '@atomic-layout/core'

/**
* Returns a list of breakpoints that match the current state of the viewport.
*/
export default function useCurrentBreakpoints(): string[] {
Copy link
Owner Author

@kettanaito kettanaito Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a list of breakpoint names, because it's possible for multiple breakpoints to match a single state of the viewport:

Layout.configure({
  breakpoints: {
    portrait: {
      orientation: 'portrait'
    },
    mobile: {
      maxWidth: 576
    }
  }
})

On a viewport with the width of 500px both portrait and mobile breakpoints would match.

const [currentBreakpoints, setCurrentBreakpoints] = useState<string[]>([
Layout.defaultBreakpointName,
])

useViewportChange(() => {
const matchingBreakpoints = Object.keys(Layout.breakpoints).filter(
(breakpointName) => {
const mediaQueryObject = Layout.breakpoints[breakpointName]
/**
* @fixme Move the media query composition and matching logic
* into the `Layout` class.
* @reason It's expensive and redundant to compose strings from
* breakpoints on each viewport change. Breakpoints never change
* on runtime.
*/
const mediaQuery = createMediaQuery(mediaQueryObject, 'only')
return matchMedia(mediaQuery).matches
},
)

setCurrentBreakpoints(matchingBreakpoints)
})

return currentBreakpoints
}
1 change: 1 addition & 0 deletions packages/atomic-layout/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { default as Visible } from './components/Visible'
export { useMediaQuery } from './hooks/useMediaQuery'
export { default as useViewportChange } from './hooks/useViewportChange'
export { default as useBreakpointChange } from './hooks/useBreakpointChange'
export { default as useCurrentBreakpoints } from './hooks/useCurrentBreakpoints'
export { default as useResponsiveValue } from './hooks/useResponsiveValue'
export { default as useResponsiveProps } from './hooks/useResponsiveProps'
export { default as useResponsiveQuery } from './hooks/useResponsiveQuery'
Expand Down