-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vote theme (options: lemmy, reddit) (#1440)
- Loading branch information
Showing
9 changed files
with
190 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ComponentProps, useMemo } from "react"; | ||
import { useAppSelector } from "../../store"; | ||
import Stat from "../post/detail/Stat"; | ||
import { | ||
VOTE_COLORS, | ||
bgColorToVariable, | ||
} from "../settings/appearance/themes/votesTheme/VotesTheme"; | ||
|
||
interface VoteStatProps extends ComponentProps<typeof Stat> { | ||
currentVote: 1 | -1 | 0; | ||
voteRepresented?: 1 | -1 | 0; | ||
} | ||
|
||
export default function VoteStat({ | ||
currentVote, | ||
voteRepresented, | ||
...props | ||
}: VoteStatProps) { | ||
const votesTheme = useAppSelector( | ||
(state) => state.settings.appearance.votesTheme, | ||
); | ||
|
||
const color = useMemo(() => { | ||
if (voteRepresented === undefined || currentVote === voteRepresented) { | ||
switch (currentVote) { | ||
case 1: | ||
return bgColorToVariable(VOTE_COLORS.UPVOTE[votesTheme]); | ||
case -1: | ||
return bgColorToVariable(VOTE_COLORS.DOWNVOTE[votesTheme]); | ||
} | ||
} | ||
|
||
return "inherit"; | ||
}, [currentVote, voteRepresented, votesTheme]); | ||
|
||
return <Stat {...props} style={{ color }} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/features/settings/appearance/themes/votesTheme/VotesTheme.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
IonIcon, | ||
IonItem, | ||
IonLabel, | ||
IonList, | ||
IonRadio, | ||
IonRadioGroup, | ||
} from "@ionic/react"; | ||
import { ListHeader } from "../../../shared/formatting"; | ||
import { OVotesThemeType } from "../../../../../services/db"; | ||
import { styled } from "@linaria/react"; | ||
import { css } from "@linaria/core"; | ||
import { useAppDispatch, useAppSelector } from "../../../../../store"; | ||
import { startCase } from "lodash"; | ||
import { arrowDownOutline, arrowUpOutline } from "ionicons/icons"; | ||
import { setVotesTheme } from "../../../settingsSlice"; | ||
|
||
export const VOTE_COLORS = { | ||
UPVOTE: { | ||
[OVotesThemeType.Lemmy]: "primary-fixed", | ||
[OVotesThemeType.Reddit]: "reddit-upvote", | ||
}, | ||
DOWNVOTE: { | ||
[OVotesThemeType.Lemmy]: "danger", | ||
[OVotesThemeType.Reddit]: "primary-fixed", | ||
}, | ||
}; | ||
|
||
export function bgColorToVariable(bgColor: string): string { | ||
return `var(--ion-color-${bgColor})`; | ||
} | ||
|
||
const VotesContainer = styled.div` | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
`; | ||
|
||
const Votes = styled.div` | ||
display: flex; | ||
gap: 6px; | ||
margin: 0 6px; | ||
`; | ||
|
||
const Vote = styled.div<{ bgColor: string }>` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding: 4px; | ||
border-radius: 4px; | ||
background: ${({ bgColor }) => bgColorToVariable(bgColor)}; | ||
color: white; | ||
`; | ||
|
||
export default function VotesTheme() { | ||
const dispatch = useAppDispatch(); | ||
const votesTheme = useAppSelector( | ||
(state) => state.settings.appearance.votesTheme, | ||
); | ||
|
||
return ( | ||
<> | ||
<ListHeader> | ||
<IonLabel>Votes Theme</IonLabel> | ||
</ListHeader> | ||
<IonRadioGroup | ||
value={votesTheme} | ||
onIonChange={(e) => { | ||
dispatch(setVotesTheme(e.detail.value)); | ||
}} | ||
> | ||
<IonList inset> | ||
{Object.entries(OVotesThemeType).map(([label, value]) => ( | ||
<IonItem key={value}> | ||
<IonRadio | ||
value={value} | ||
className={css` | ||
&::part(label) { | ||
flex: 1; | ||
} | ||
`} | ||
> | ||
<VotesContainer> | ||
<IonLabel>{startCase(label)}</IonLabel> | ||
<Votes> | ||
<Vote bgColor={VOTE_COLORS.UPVOTE[value]}> | ||
<IonIcon icon={arrowUpOutline} /> | ||
</Vote> | ||
<Vote bgColor={VOTE_COLORS.DOWNVOTE[value]}> | ||
<IonIcon icon={arrowDownOutline} /> | ||
</Vote> | ||
</Votes> | ||
</VotesContainer> | ||
</IonRadio> | ||
</IonItem> | ||
))} | ||
</IonList> | ||
</IonRadioGroup> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.