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

Refactored breakpoints #307

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
04bee8d
done
DrShpongle Jan 19, 2021
04f3691
fix condition
DrShpongle Jan 19, 2021
071b2bc
fix condition
DrShpongle Jan 19, 2021
4a281fe
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 19, 2021
7a1cf6d
Merge branch 'main' of github.com:eggheadio/egghead-next into en/brea…
DrShpongle Jan 19, 2021
986b6a9
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 19, 2021
c61a03b
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 20, 2021
14f6fef
Merge branch 'en/breakpoints-refactoring' of github.com:eggheadio/egg…
DrShpongle Jan 20, 2021
10d555c
refactoring
DrShpongle Jan 20, 2021
584bc53
clean up
DrShpongle Jan 20, 2021
eb06541
Merge branch 'main' of github.com:eggheadio/egghead-next into en/brea…
DrShpongle Jan 20, 2021
38144f8
add zero breakpoint
DrShpongle Jan 20, 2021
18c8a74
Merge branch 'main' of github.com:eggheadio/egghead-next into en/brea…
DrShpongle Jan 20, 2021
7828ffe
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 20, 2021
a09ad6f
Merge branch 'main' of github.com:eggheadio/egghead-next into en/brea…
DrShpongle Jan 21, 2021
c3ef778
Merge branch 'main' of github.com:eggheadio/egghead-next into en/brea…
DrShpongle Jan 22, 2021
a58d8a4
Merge branch 'main' of github.com:eggheadio/egghead-next into en/brea…
DrShpongle Jan 22, 2021
3941be6
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 23, 2021
7fabcf0
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 24, 2021
17fa049
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 24, 2021
61ac79b
Merge branch 'main' into en/breakpoints-refactoring
DrShpongle Jan 24, 2021
8bcf8c3
Merge branch 'main' into en/breakpoints-refactoring
vojtaholik Jan 24, 2021
f25fb81
Merge branch 'main' into en/breakpoints-refactoring
vojtaholik Jan 26, 2021
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
14 changes: 8 additions & 6 deletions src/components/app/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {useViewer} from 'context/viewer-context'
import {track} from 'utils/analytics'
import {isEmpty} from 'lodash'
import Feedback from 'components/feedback-input'
import useBreakpoint from 'utils/breakpoints'
import breakpoints from 'utils/breakpoints'
import {useRouter} from 'next/router'
import {useTheme} from 'next-themes'

Expand All @@ -16,12 +16,14 @@ const ACCOUNT_LINK_ENABLED =
const Header: FunctionComponent = () => {
const router = useRouter()
const {viewer, loading} = useViewer()
const {sm} = useBreakpoint()
const {isMinSM} = breakpoints()
const [isOpen, setOpen] = React.useState<boolean>(false)

React.useEffect(() => {
!sm ? setOpen(sm) : setOpen(false)
}, [sm, router])
if (isMinSM) {
setOpen(false)
}
}, [isMinSM, router])

const Navigation: FunctionComponent<{
className?: string
Expand Down Expand Up @@ -160,8 +162,8 @@ const Header: FunctionComponent = () => {
</a>
</Link>
</div>
{!sm && <Navigation />}
{sm && !loading && (
{isMinSM && <Navigation />}
{!isMinSM && !loading && (
<button
onClick={() => setOpen(!isOpen)}
aria-labelledby="menubutton"
Expand Down
10 changes: 5 additions & 5 deletions src/pages/lessons/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {track} from 'utils/analytics'
import Eggo from 'components/icons/eggo'
import Image from 'next/image'
import cookieUtil from 'utils/cookies'
import useBreakpoint from 'utils/breakpoints'
import breakpoints from 'utils/breakpoints'
import Share from 'components/share'
import LessonDownload from 'components/pages/lessons/lesson-download'
import {useNextForCollection} from 'hooks/use-next-up-data'
Expand Down Expand Up @@ -124,7 +124,7 @@ const Lesson: FunctionComponent<LessonProps> = ({initialLesson}) => {
const {viewer} = useViewer()
const {setPlayerPrefs, playbackRate, defaultView} = useEggheadPlayerPrefs()

const {md} = useBreakpoint()
const {isMinMD} = breakpoints()

const {height} = useWindowSize()
const CONTENT_OFFSET = height < 450 ? 30 : 120
Expand Down Expand Up @@ -677,9 +677,9 @@ const Lesson: FunctionComponent<LessonProps> = ({initialLesson}) => {
</div>
</div>
)}
{!md && <Tags tags={collectionTags} lesson={lesson} />}
{!isMinMD && <Tags tags={collectionTags} lesson={lesson} />}
</div>
{md && <Tags tags={collectionTags} lesson={lesson} />}
{isMinMD && <Tags tags={collectionTags} lesson={lesson} />}
<div className="flex items-center space-x-8">
<div className="flex flex-col md:flex-row items-center space-y-2 md:space-y-0 md:space-x-2">
<Share
Expand Down Expand Up @@ -732,7 +732,7 @@ const Lesson: FunctionComponent<LessonProps> = ({initialLesson}) => {
</Markdown>
)}
</div>
{md && (
{isMinMD && (
<>
<Course course={collection} currentLessonSlug={lesson.slug} />

Expand Down
25 changes: 11 additions & 14 deletions src/utils/breakpoints.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {createBreakpoint} from 'react-use'

const bp: any = {
_: 0,
xs: 375,
sm: 640,
md: 768,
Expand All @@ -20,20 +21,16 @@ export const bpMaxSM = `@media (max-width: ${bp.md - 1}px)`
export const bpMaxMD = `@media (max-width: ${bp.lg - 1}px)`
export const bpMaxLG = `@media (max-width: ${bp.xl - 1}px)`

const useBreakpoints = createBreakpoint(bp)
const getBreakpoint = createBreakpoint(bp)

const useBreakpoint = () => {
const breakpoint = useBreakpoints()

const xs = breakpoint == 'xs'
const sm = breakpoint == 'sm' || xs
const md = breakpoint == 'md' || sm
const lg = breakpoint == 'lg' || md
const xl = breakpoint == 'xl' || lg
const xl2 = breakpoint == '2xl' || xl
const xl3 = breakpoint == '3xl' || xl2

return {xs, sm, md, lg, xl, xl2, xl3}
const breakpoints = () => {
const breakpoint = getBreakpoint()
const keys = Object.keys(bp)
const foundInd = keys.indexOf(breakpoint)
return keys
.map((key, ind) => ({['isMin' + key.toUpperCase()]: ind <= foundInd}))
.filter((_, ind) => ind)
Copy link
Contributor Author

@DrShpongle DrShpongle Jan 20, 2021

Choose a reason for hiding this comment

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

Idea here is to filter out first zero value, because it's like default class in tailwindcss (flex-row vs sm:flex-row)

.reduce((acc, cur) => ({...acc, ...cur}), {})
}
Copy link
Contributor Author

@DrShpongle DrShpongle Jan 20, 2021

Choose a reason for hiding this comment

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

Eventually we get the object like:
for the xl breakpoint

{
  isMinXS: true,
  isMinSM: true,
  isMinMD: true,
  isMinLG: true,
  isMinXL: true,
  isMin2XL: false,
  isMin3XL: false,
}

"only mobile" breakpoint could be considered as !isMinXS or !isMinSM (portrait/landscape)

so, if we want to show some component on mobiles only we want to !isMinXS && <Component />
if we want to show component on md and bigger (768+) we should to isMinMD && <Component />


export default useBreakpoint
export default breakpoints