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

Refactor sidebar #6971

Merged
merged 17 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
28 changes: 28 additions & 0 deletions src/alf/breakpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {useMemo} from 'react'
import {useMediaQuery} from 'react-responsive'

export type Breakpoint = 'gtPhone' | 'gtMobile' | 'gtTablet'

export function useBreakpoints(): Record<Breakpoint, boolean> & {
activeBreakpoint: Breakpoint | undefined
} {
const gtPhone = useMediaQuery({minWidth: 500})
const gtMobile = useMediaQuery({minWidth: 800})
const gtTablet = useMediaQuery({minWidth: 1300})
return useMemo(() => {
let active: Breakpoint | undefined
if (gtTablet) {
active = 'gtTablet'
} else if (gtMobile) {
active = 'gtMobile'
} else if (gtPhone) {
active = 'gtPhone'
}
return {
activeBreakpoint: active,
gtPhone,
gtMobile,
gtTablet,
}
}, [gtPhone, gtMobile, gtTablet])
}
15 changes: 2 additions & 13 deletions src/alf/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import {useMediaQuery} from 'react-responsive'

import {
computeFontScaleMultiplier,
Expand All @@ -14,13 +13,14 @@
import {Device} from '#/storage'

export {atoms} from '#/alf/atoms'
export * from '#/alf/breakpoints'
export * from '#/alf/fonts'
export * as tokens from '#/alf/tokens'
export * from '#/alf/types'
export * from '#/alf/util/flatten'
export * from '#/alf/util/platform'
export * from '#/alf/util/themeSelector'
export * from '#/alf/util/useGutterStyles'
export * from '#/alf/util/useGutters'

export type Alf = {
themeName: ThemeName
Expand Down Expand Up @@ -75,7 +75,7 @@
const setFontScaleAndPersist = React.useCallback<
Alf['fonts']['setFontScale']
>(
fontScale => {

Check warning on line 78 in src/alf/index.tsx

View workflow job for this annotation

GitHub Actions / Run linters

'fontScale' is already declared in the upper scope on line 69 column 10
setFontScale(fontScale)
persistFontScale(fontScale)
setFontScaleMultiplier(computeFontScaleMultiplier(fontScale))
Expand All @@ -88,7 +88,7 @@
const setFontFamilyAndPersist = React.useCallback<
Alf['fonts']['setFontFamily']
>(
fontFamily => {

Check warning on line 91 in src/alf/index.tsx

View workflow job for this annotation

GitHub Actions / Run linters

'fontFamily' is already declared in the upper scope on line 85 column 10
setFontFamily(fontFamily)
persistFontFamily(fontFamily)
},
Expand Down Expand Up @@ -142,14 +142,3 @@
return theme ? alf.themes[theme] : alf.theme
}, [theme, alf])
}

export function useBreakpoints() {
const gtPhone = useMediaQuery({minWidth: 500})
const gtMobile = useMediaQuery({minWidth: 800})
const gtTablet = useMediaQuery({minWidth: 1300})
return {
gtPhone,
gtMobile,
gtTablet,
}
}
21 changes: 0 additions & 21 deletions src/alf/util/useGutterStyles.ts

This file was deleted.

68 changes: 68 additions & 0 deletions src/alf/util/useGutters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react'

import {Breakpoint, useBreakpoints} from '#/alf/breakpoints'
import * as tokens from '#/alf/tokens'

type Gutter = 'compact' | 'base' | 'wide' | 0

const gutters: Record<
Exclude<Gutter, 0>,
Record<Breakpoint | 'default', number>
> = {
compact: {
default: tokens.space.sm,
gtPhone: tokens.space.sm,
gtMobile: tokens.space.md,
gtTablet: tokens.space.md,
},
base: {
default: tokens.space.lg,
gtPhone: tokens.space.lg,
gtMobile: tokens.space.xl,
gtTablet: tokens.space.xl,
},
wide: {
default: tokens.space.xl,
gtPhone: tokens.space.xl,
gtMobile: tokens.space._3xl,
gtTablet: tokens.space._3xl,
},
}

type Gutters = {
paddingTop: number
paddingRight: number
paddingBottom: number
paddingLeft: number
}

export function useGutters([all]: [Gutter]): Gutters
export function useGutters([vertical, horizontal]: [Gutter, Gutter]): Gutters
export function useGutters([top, right, bottom, left]: [
Gutter,
Gutter,
Gutter,
Gutter,
]): Gutters
export function useGutters([top, right, bottom, left]: Gutter[]) {
const {activeBreakpoint} = useBreakpoints()
return React.useMemo(() => {
if (!right) {
// eslint-disable-next-line react-hooks/exhaustive-deps
right = bottom = left = top
} else if (!bottom || !left) {
bottom = top
left = right
}

return {
paddingTop: top === 0 ? 0 : gutters[top][activeBreakpoint || 'default'],
paddingRight:
right === 0 ? 0 : gutters[right][activeBreakpoint || 'default'],
paddingBottom:
bottom === 0 ? 0 : gutters[bottom][activeBreakpoint || 'default'],
paddingLeft:
left === 0 ? 0 : gutters[left][activeBreakpoint || 'default'],
}
}, [activeBreakpoint, top, bottom, left, right])
}
6 changes: 3 additions & 3 deletions src/components/Layout/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
platform,
TextStyleProp,
useBreakpoints,
useGutterStyles,
useGutters,
useTheme,
} from '#/alf'
import {Button, ButtonIcon, ButtonProps} from '#/components/Button'
Expand All @@ -34,7 +34,7 @@ export function Outer({
noBottomBorder?: boolean
}) {
const t = useTheme()
const gutter = useGutterStyles()
const gutters = useGutters([0, 'base'])
Copy link
Member Author

Choose a reason for hiding this comment

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

No change here from previous styles

const {gtMobile} = useBreakpoints()
const {isWithinOffsetView} = useContext(ScrollbarOffsetContext)

Expand All @@ -46,7 +46,7 @@ export function Outer({
a.flex_row,
a.align_center,
a.gap_sm,
gutter,
gutters,
platform({
native: [a.pb_sm, a.pt_xs],
web: [a.py_sm],
Expand Down
4 changes: 3 additions & 1 deletion src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ export function Link({
}

export type InlineLinkProps = React.PropsWithChildren<
BaseLinkProps & TextStyleProp & Pick<TextProps, 'selectable'>
BaseLinkProps &
TextStyleProp &
Pick<TextProps, 'selectable' | 'numberOfLines'>
> &
Pick<ButtonProps, 'label'> & {
disableUnderline?: boolean
Expand Down
6 changes: 3 additions & 3 deletions src/view/com/home/HomeHeaderLayout.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {useSession} from '#/state/session'
import {useShellLayout} from '#/state/shell/shell-layout'
import {HomeHeaderLayoutMobile} from '#/view/com/home/HomeHeaderLayoutMobile'
import {Logo} from '#/view/icons/Logo'
import {atoms as a, useBreakpoints, useGutterStyles, useTheme} from '#/alf'
import {atoms as a, useBreakpoints, useGutters, useTheme} from '#/alf'
import {ButtonIcon} from '#/components/Button'
import {Hashtag_Stroke2_Corner0_Rounded as FeedsIcon} from '#/components/icons/Hashtag'
import * as Layout from '#/components/Layout'
Expand Down Expand Up @@ -41,14 +41,14 @@ function HomeHeaderLayoutDesktopAndTablet({
const {hasSession} = useSession()
const {_} = useLingui()
const kawaii = useKawaiiMode()
const gutter = useGutterStyles()
const gutters = useGutters([0, 'base'])
Copy link
Member Author

Choose a reason for hiding this comment

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

No change here from previous styles


return (
<>
{hasSession && (
<Layout.Center>
<View
style={[a.flex_row, a.align_center, a.pt_md, gutter, t.atoms.bg]}>
style={[a.flex_row, a.align_center, a.pt_md, gutters, t.atoms.bg]}>
<View style={{width: 34}} />
<View style={[a.flex_1, a.align_center, a.justify_center]}>
<Logo width={kawaii ? 60 : 28} />
Expand Down
101 changes: 44 additions & 57 deletions src/view/shell/desktop/Feeds.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {StyleSheet, View} from 'react-native'
import {View} from 'react-native'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useNavigation, useNavigationState} from '@react-navigation/native'

import {usePalette} from '#/lib/hooks/usePalette'
import {getCurrentRoute} from '#/lib/routes/helpers'
import {NavigationProp} from '#/lib/routes/types'
import {emitSoftReset} from '#/state/events'
import {usePinnedFeedsInfos} from '#/state/queries/feed'
import {useSelectedFeed, useSetSelectedFeed} from '#/state/shell/selected-feed'
import {TextLink} from '#/view/com/util/Link'
import {atoms as a, useTheme, web} from '#/alf'
import {SquareArrowTopRight_Stroke2_Corner0_Rounded as Link} from '#/components/icons/SquareArrowTopRight'
import {InlineLinkText} from '#/components/Link'

export function DesktopFeeds() {
const pal = usePalette('default')
const t = useTheme()
const {_} = useLingui()
const {data: pinnedFeedInfos} = usePinnedFeedsInfos()
const selectedFeed = useSelectedFeed()
Expand All @@ -24,76 +25,62 @@ export function DesktopFeeds() {
}
return getCurrentRoute(state)
})

if (!pinnedFeedInfos) {
return null
}

return (
<View style={[styles.container, pal.view]}>
<View
style={[
a.flex_1,
a.gap_sm,
web({
/*
* Small padding prevents overflow prior to actually overflowing the
* height of the screen with lots of feeds.
*/
paddingVertical: 2,
overflowY: 'auto',
}),
]}>
{pinnedFeedInfos.map(feedInfo => {
const feed = feedInfo.feedDescriptor
const current = route.name === 'Home' && feed === selectedFeed

return (
<FeedItem
key={feed}
href={'/?' + new URLSearchParams([['feed', feed]])}
title={feedInfo.displayName}
current={route.name === 'Home' && feed === selectedFeed}
<InlineLinkText
key={feedInfo.uri}
to={'/?' + new URLSearchParams([['feed', feed]])}
label={feedInfo.displayName}
onPress={() => {
setSelectedFeed(feed)
navigation.navigate('Home')
if (route.name === 'Home' && feed === selectedFeed) {
emitSoftReset()
}
}}
/>
style={[
a.text_md,
a.leading_snug,
current
? [a.font_heavy, t.atoms.text]
: [t.atoms.text_contrast_medium],
]}
numberOfLines={1}>
{feedInfo.displayName}
</InlineLinkText>
)
})}
<View style={{paddingTop: 8, paddingBottom: 6}}>
<TextLink
type="lg"
href="/feeds"
text={_(msg`More feeds`)}
style={[pal.link]}
/>
</View>
</View>
)
}

function FeedItem({
title,
href,
current,
onPress,
}: {
title: string
href: string
current: boolean
onPress: () => void
}) {
const pal = usePalette('default')
return (
<View style={{paddingVertical: 6}}>
<TextLink
type="xl"
href={href}
text={title}
onPress={onPress}
style={[
current ? pal.text : pal.textLight,
{letterSpacing: 0.15, fontWeight: current ? '600' : '400'},
]}
/>
<InlineLinkText
to="/feeds"
label={_(msg`More feeds`)}
style={[a.text_md, a.leading_snug]}
numberOfLines={1}>
{_(msg`More feeds`)}
<Link width={16} style={[a.relative, {marginBottom: -2, right: -4}]} />
</InlineLinkText>
</View>
)
}

const styles = StyleSheet.create({
container: {
flex: 1,
// @ts-ignore web only -prf
overflowY: 'auto',
width: 300,
paddingHorizontal: 12,
paddingVertical: 18,
},
})
Loading
Loading