Skip to content

Commit

Permalink
Merge pull request #3619 from omnivore-app/feat/web-rtl-text
Browse files Browse the repository at this point in the history
Allow setting text as RTL in the reader
  • Loading branch information
jacksonh authored Mar 4, 2024
2 parents 07f771c + b7cc861 commit 14bcf40
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/web/components/templates/article/ArticleContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ArticleAttributes } from '../../../lib/networking/queries/useGetArticleQuery'
import {
ArticleAttributes,
TextDirection,
} from '../../../lib/networking/queries/useGetArticleQuery'
import { Article } from './../../../components/templates/article/Article'
import { Box, HStack, SpanBox, VStack } from './../../elements/LayoutPrimitives'
import { StyledText } from './../../elements/StyledText'
Expand Down Expand Up @@ -38,6 +41,7 @@ type ArticleContainerProps = {
showHighlightsModal: boolean
highlightOnRelease?: boolean
justifyText?: boolean
textDirection?: TextDirection
setShowHighlightsModal: React.Dispatch<React.SetStateAction<boolean>>
}

Expand Down Expand Up @@ -138,6 +142,9 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
const highlightHref = useRef(
window.location.hash ? window.location.hash.split('#')[1] : null
)
const [textDirection, setTextDirection] = useState(
props.textDirection ?? 'LTR'
)

const updateFontSize = useCallback(
(newFontSize: number) => {
Expand Down Expand Up @@ -173,6 +180,14 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
setHighlightOnRelease(isEnabled)
}

interface UpdateTextDirectionEvent extends Event {
textDirection: TextDirection
}

const handleUpdateTextDirection = (event: UpdateTextDirectionEvent) => {
setTextDirection(event.textDirection)
}

interface UpdateMaxWidthPercentageEvent extends Event {
maxWidthPercentage?: number
}
Expand Down Expand Up @@ -367,6 +382,7 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element {
return (
<>
<Box
dir={textDirection}
id="article-container"
css={{
padding: 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,37 @@ function AdvancedSettings(props: SettingsProps): JSX.Element {
</SwitchRoot>
</HStack>

<HStack
css={{
width: '100%',
pr: '30px',
alignItems: 'center',
'&:hover': {
opacity: 0.8,
},
'&[data-state="on"]': {
bg: '$thBackground',
},
}}
alignment="start"
distribution="between"
>
<Label htmlFor="auto-highlight-mode" css={{ width: '100%' }}>
<StyledText style="displaySettingsLabel" css={{ pl: '20px' }}>
Right-to-left text
</StyledText>
</Label>
<SwitchRoot
id="rtl-text"
checked={readerSettings.textDirection == 'RTL'}
onCheckedChange={(checked) => {
readerSettings.setTextDirection(checked ? 'RTL' : 'LTR')
}}
>
<SwitchThumb />
</SwitchRoot>
</HStack>

<HStack
css={{
width: '100%',
Expand Down
12 changes: 12 additions & 0 deletions packages/web/lib/hooks/useReaderSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useRegisterActions } from 'kbar'
import { useCallback, useState } from 'react'
import { applyStoredTheme } from '../themeUpdater'
import { usePersistedState } from './usePersistedState'
import { TextDirection } from '../networking/queries/useGetArticleQuery'

const DEFAULT_FONT = 'Inter'

Expand Down Expand Up @@ -34,6 +35,9 @@ export type ReaderSettings = {

highlightOnRelease: boolean | undefined
setHighlightOnRelease: (set: boolean) => void

textDirection: TextDirection | undefined
setTextDirection: (textDirection: TextDirection) => void
}

export const useReaderSettings = (): ReaderSettings => {
Expand Down Expand Up @@ -73,6 +77,12 @@ export const useReaderSettings = (): ReaderSettings => {
key: `--display-justify-text`,
initialValue: false,
})
const [textDirection, setTextDirection] = usePersistedState<
TextDirection | undefined
>({
key: `--display-text-direction`,
initialValue: 'LTR',
})
const [showSetLabelsModal, setShowSetLabelsModal] = useState(false)
const [showEditDisplaySettingsModal, setShowEditDisplaySettingsModal] =
useState(false)
Expand Down Expand Up @@ -226,5 +236,7 @@ export const useReaderSettings = (): ReaderSettings => {
setHighContrastText,
highlightOnRelease,
setHighlightOnRelease,
textDirection,
setTextDirection,
}
}
3 changes: 3 additions & 0 deletions packages/web/lib/networking/queries/useGetArticleQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type NestedArticleData = {
errorCodes?: string[]
}

export type TextDirection = 'RTL' | 'LTR'

export type ArticleAttributes = {
id: string
title: string
Expand All @@ -64,6 +66,7 @@ export type ArticleAttributes = {
linkId: string
labels?: Label[]
state?: State
directionality?: TextDirection
recommendations?: Recommendation[]
}

Expand Down
1 change: 1 addition & 0 deletions packages/web/pages/[username]/[slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ export default function Home(): JSX.Element {
highlightOnRelease={
readerSettings.highlightOnRelease ?? undefined
}
textDirection={readerSettings.textDirection}
articleMutations={{
createHighlightMutation,
deleteHighlightMutation,
Expand Down
2 changes: 1 addition & 1 deletion packages/web/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Document extends NextDocument {
globalStyles()

return (
<Html lang="en">
<Html lang="en" dir="ltr">
<Head>
<style
id="stitches"
Expand Down

0 comments on commit 14bcf40

Please sign in to comment.