pass theme and colorscheme in tss context to avoid repetitions (#1241)

This commit is contained in:
Athou
2024-02-12 07:37:28 +01:00
parent 3efeed6c85
commit b6107c3330
9 changed files with 48 additions and 95 deletions

View File

@@ -1,5 +1,4 @@
import { Box, Center, type MantineTheme, useMantineTheme } from "@mantine/core" import { Box, Center } from "@mantine/core"
import { useColorScheme } from "hooks/useColorScheme"
import { useState } from "react" import { useState } from "react"
import { TbPhoto } from "react-icons/tb" import { TbPhoto } from "react-icons/tb"
import { tss } from "tss" import { tss } from "tss"
@@ -18,8 +17,6 @@ interface ImageWithPlaceholderWhileLoadingProps {
const useStyles = tss const useStyles = tss
.withParams<{ .withParams<{
theme: MantineTheme
colorScheme: "light" | "dark"
placeholderWidth?: number placeholderWidth?: number
placeholderHeight?: number placeholderHeight?: number
placeholderBackgroundColor?: string placeholderBackgroundColor?: string
@@ -46,11 +43,7 @@ export function ImageWithPlaceholderWhileLoading({
title, title,
width, width,
}: ImageWithPlaceholderWhileLoadingProps) { }: ImageWithPlaceholderWhileLoadingProps) {
const theme = useMantineTheme()
const colorScheme = useColorScheme()
const { classes } = useStyles({ const { classes } = useStyles({
theme,
colorScheme,
placeholderWidth, placeholderWidth,
placeholderHeight, placeholderHeight,
placeholderBackgroundColor, placeholderBackgroundColor,

View File

@@ -1,7 +1,6 @@
import { Box, Divider, type MantineRadius, type MantineSpacing, type MantineTheme, Paper, useMantineTheme } from "@mantine/core" import { Box, Divider, type MantineRadius, type MantineSpacing, Paper } from "@mantine/core"
import { Constants } from "app/constants" import { Constants } from "app/constants"
import { type Entry, type ViewMode } from "app/types" import { type Entry, type ViewMode } from "app/types"
import { useColorScheme } from "hooks/useColorScheme"
import { useViewMode } from "hooks/useViewMode" import { useViewMode } from "hooks/useViewMode"
import React from "react" import React from "react"
import { useSwipeable } from "react-swipeable" import { useSwipeable } from "react-swipeable"
@@ -26,8 +25,6 @@ interface FeedEntryProps {
const useStyles = tss const useStyles = tss
.withParams<{ .withParams<{
theme: MantineTheme
colorScheme: "light" | "dark"
read: boolean read: boolean
expanded: boolean expanded: boolean
viewMode: ViewMode viewMode: ViewMode
@@ -96,12 +93,8 @@ const useStyles = tss
}) })
export function FeedEntry(props: FeedEntryProps) { export function FeedEntry(props: FeedEntryProps) {
const theme = useMantineTheme()
const colorScheme = useColorScheme()
const { viewMode } = useViewMode() const { viewMode } = useViewMode()
const { classes, cx } = useStyles({ const { classes, cx } = useStyles({
theme,
colorScheme,
read: props.entry.read, read: props.entry.read,
expanded: props.expanded, expanded: props.expanded,
viewMode, viewMode,

View File

@@ -2,7 +2,6 @@ import { Box, Text } from "@mantine/core"
import { type Entry } from "app/types" import { type Entry } from "app/types"
import { RelativeDate } from "components/RelativeDate" import { RelativeDate } from "components/RelativeDate"
import { OnDesktop } from "components/responsive/OnDesktop" import { OnDesktop } from "components/responsive/OnDesktop"
import { useColorScheme } from "hooks/useColorScheme"
import { tss } from "tss" import { tss } from "tss"
import { FeedEntryTitle } from "./FeedEntryTitle" import { FeedEntryTitle } from "./FeedEntryTitle"
import { FeedFavicon } from "./FeedFavicon" import { FeedFavicon } from "./FeedFavicon"
@@ -13,7 +12,6 @@ export interface FeedEntryHeaderProps {
const useStyles = tss const useStyles = tss
.withParams<{ .withParams<{
colorScheme: "light" | "dark"
read: boolean read: boolean
}>() }>()
.create(({ colorScheme, read }) => ({ .create(({ colorScheme, read }) => ({
@@ -42,9 +40,7 @@ const useStyles = tss
})) }))
export function FeedEntryCompactHeader(props: FeedEntryHeaderProps) { export function FeedEntryCompactHeader(props: FeedEntryHeaderProps) {
const colorScheme = useColorScheme()
const { classes } = useStyles({ const { classes } = useStyles({
colorScheme,
read: props.entry.read, read: props.entry.read,
}) })
return ( return (

View File

@@ -1,5 +1,5 @@
import { Trans } from "@lingui/macro" import { Trans } from "@lingui/macro"
import { Group, type MantineTheme, useMantineTheme } from "@mantine/core" import { Group } from "@mantine/core"
import { Constants } from "app/constants" import { Constants } from "app/constants"
import { markEntriesUpToEntry, markEntry, starEntry } from "app/entries/thunks" import { markEntriesUpToEntry, markEntry, starEntry } from "app/entries/thunks"
import { redirectToFeed } from "app/redirect/thunks" import { redirectToFeed } from "app/redirect/thunks"
@@ -17,12 +17,7 @@ interface FeedEntryContextMenuProps {
} }
const iconSize = 16 const iconSize = 16
const useStyles = tss const useStyles = tss.create(({ theme, colorScheme }) => ({
.withParams<{
theme: MantineTheme
colorScheme: "light" | "dark"
}>()
.create(({ theme, colorScheme }) => ({
menu: { menu: {
// apply mantine theme from MenuItem.styles.ts // apply mantine theme from MenuItem.styles.ts
fontSize: theme.fontSizes.sm, fontSize: theme.fontSizes.sm,
@@ -30,15 +25,11 @@ const useStyles = tss
"--contexify-activeItem-color": `${colorScheme === "dark" ? theme.colors.dark[0] : theme.black} !important`, "--contexify-activeItem-color": `${colorScheme === "dark" ? theme.colors.dark[0] : theme.black} !important`,
"--contexify-activeItem-bgColor": `${colorScheme === "dark" ? theme.colors.dark[4] : theme.colors.gray[1]} !important`, "--contexify-activeItem-bgColor": `${colorScheme === "dark" ? theme.colors.dark[4] : theme.colors.gray[1]} !important`,
}, },
})) }))
export function FeedEntryContextMenu(props: FeedEntryContextMenuProps) { export function FeedEntryContextMenu(props: FeedEntryContextMenuProps) {
const theme = useMantineTheme()
const colorScheme = useColorScheme() const colorScheme = useColorScheme()
const { classes } = useStyles({ const { classes } = useStyles()
theme,
colorScheme,
})
const sourceType = useAppSelector(state => state.entries.source.type) const sourceType = useAppSelector(state => state.entries.source.type)
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const { openLinkInBackgroundTab } = useBrowserExtension() const { openLinkInBackgroundTab } = useBrowserExtension()

View File

@@ -1,7 +1,6 @@
import { Box, Space, Text } from "@mantine/core" import { Box, Space, Text } from "@mantine/core"
import { type Entry } from "app/types" import { type Entry } from "app/types"
import { RelativeDate } from "components/RelativeDate" import { RelativeDate } from "components/RelativeDate"
import { useColorScheme } from "hooks/useColorScheme"
import { tss } from "tss" import { tss } from "tss"
import { FeedEntryTitle } from "./FeedEntryTitle" import { FeedEntryTitle } from "./FeedEntryTitle"
import { FeedFavicon } from "./FeedFavicon" import { FeedFavicon } from "./FeedFavicon"
@@ -13,7 +12,6 @@ export interface FeedEntryHeaderProps {
const useStyles = tss const useStyles = tss
.withParams<{ .withParams<{
colorScheme: "light" | "dark"
read: boolean read: boolean
}>() }>()
.create(({ colorScheme, read }) => ({ .create(({ colorScheme, read }) => ({
@@ -28,9 +26,7 @@ const useStyles = tss
})) }))
export function FeedEntryHeader(props: FeedEntryHeaderProps) { export function FeedEntryHeader(props: FeedEntryHeaderProps) {
const colorScheme = useColorScheme()
const { classes } = useStyles({ const { classes } = useStyles({
colorScheme,
read: props.entry.read, read: props.entry.read,
}) })
return ( return (

View File

@@ -1,8 +1,7 @@
import { ActionIcon, Box, type MantineTheme, SimpleGrid, useMantineTheme } from "@mantine/core" import { ActionIcon, Box, SimpleGrid } from "@mantine/core"
import { Constants } from "app/constants" import { Constants } from "app/constants"
import { useAppSelector } from "app/store" import { useAppSelector } from "app/store"
import { type SharingSettings } from "app/types" import { type SharingSettings } from "app/types"
import { useColorScheme } from "hooks/useColorScheme"
import { type IconType } from "react-icons" import { type IconType } from "react-icons"
import { tss } from "tss" import { tss } from "tss"
@@ -10,8 +9,6 @@ type Color = `#${string}`
const useStyles = tss const useStyles = tss
.withParams<{ .withParams<{
theme: MantineTheme
colorScheme: "light" | "dark"
color: Color color: Color
}>() }>()
.create(({ theme, colorScheme, color }) => ({ .create(({ theme, colorScheme, color }) => ({
@@ -23,11 +20,7 @@ const useStyles = tss
})) }))
function ShareButton({ url, icon, color }: { url: string; icon: IconType; color: Color }) { function ShareButton({ url, icon, color }: { url: string; icon: IconType; color: Color }) {
const theme = useMantineTheme()
const colorScheme = useColorScheme()
const { classes } = useStyles({ const { classes } = useStyles({
theme,
colorScheme,
color, color,
}) })

View File

@@ -1,6 +1,5 @@
import { Box, Center, type MantineTheme, useMantineTheme } from "@mantine/core" import { Box, Center } from "@mantine/core"
import { FeedFavicon } from "components/content/FeedFavicon" import { FeedFavicon } from "components/content/FeedFavicon"
import { useColorScheme } from "hooks/useColorScheme"
import React, { type ReactNode } from "react" import React, { type ReactNode } from "react"
import { tss } from "tss" import { tss } from "tss"
import { UnreadCount } from "./UnreadCount" import { UnreadCount } from "./UnreadCount"
@@ -20,8 +19,6 @@ interface TreeNodeProps {
const useStyles = tss const useStyles = tss
.withParams<{ .withParams<{
theme: MantineTheme
colorScheme: "dark" | "light"
selected: boolean selected: boolean
hasError: boolean hasError: boolean
hasUnread: boolean hasUnread: boolean
@@ -60,11 +57,7 @@ const useStyles = tss
}) })
export function TreeNode(props: TreeNodeProps) { export function TreeNode(props: TreeNodeProps) {
const theme = useMantineTheme()
const colorScheme = useColorScheme()
const { classes } = useStyles({ const { classes } = useStyles({
theme,
colorScheme,
selected: props.selected, selected: props.selected,
hasError: props.hasError, hasError: props.hasError,
hasUnread: props.unread > 0, hasUnread: props.unread > 0,

View File

@@ -1,14 +1,10 @@
import { Trans } from "@lingui/macro" import { Trans } from "@lingui/macro"
import { Box, Button, Container, Group, type MantineTheme, Text, Title, useMantineTheme } from "@mantine/core" import { Box, Button, Container, Group, Text, Title } from "@mantine/core"
import { TbRefresh } from "react-icons/tb" import { TbRefresh } from "react-icons/tb"
import { tss } from "tss" import { tss } from "tss"
import { PageTitle } from "./PageTitle" import { PageTitle } from "./PageTitle"
const useStyles = tss const useStyles = tss.create(({ theme }) => ({
.withParams<{
theme: MantineTheme
}>()
.create(({ theme }) => ({
root: { root: {
paddingTop: 80, paddingTop: 80,
}, },
@@ -34,11 +30,10 @@ const useStyles = tss
marginTop: theme.spacing.xl, marginTop: theme.spacing.xl,
marginBottom: `calc(${theme.spacing.xl} * 1.5)`, marginBottom: `calc(${theme.spacing.xl} * 1.5)`,
}, },
})) }))
export function ErrorPage(props: { error: Error }) { export function ErrorPage(props: { error: Error }) {
const theme = useMantineTheme() const { classes } = useStyles()
const { classes } = useStyles({ theme })
return ( return (
<div className={classes.root}> <div className={classes.root}>

View File

@@ -1,11 +1,14 @@
import { useMantineTheme } from "@mantine/core"
import { useColorScheme } from "hooks/useColorScheme"
import { createTss } from "tss-react" import { createTss } from "tss-react"
const useContext = () => { const useContext = () => {
// return anything here that will be accessible in tss.create() // return anything here that will be accessible in tss.create()
// we don't need anything right now
return {} const theme = useMantineTheme()
const colorScheme = useColorScheme()
return { theme, colorScheme }
} }
export const { tss } = createTss({ useContext }) export const { tss } = createTss({ useContext })
export const useStyles = tss.create({})