feat: add vote ratio bar

This commit is contained in:
James Xylight committed 2025-07-31 23:05:59 -07:00
1 parent 7ef9da9852
commit d6d1a9fe1f
6 files changed
+63 -3

No files matched your search

@@ -96,7 +96,7 @@
contentClass={[ contentClass={[
(node.children.length > 0 || (node.children.length > 0 ||
node.comment_view.counts.child_count > 0) && node.comment_view.counts.child_count > 0) &&
'border-l-2', 'border-l',
'ml-2.5 pl-3 sm:pl-4 lg:pl-5', 'ml-2.5 pl-3 sm:pl-4 lg:pl-5',
node.depth % 2 == 0 node.depth % 2 == 0
? 'border-slate-200 dark:border-zinc-800' ? 'border-slate-200 dark:border-zinc-800'
@@ -8,6 +8,7 @@
import { backOut } from 'svelte/easing' import { backOut } from 'svelte/easing'
import { fly } from 'svelte/transition' import { fly } from 'svelte/transition'
import { shouldShowVoteColor } from '../post/PostVote.svelte' import { shouldShowVoteColor } from '../post/PostVote.svelte'
import { settings } from '$lib/settings.svelte'
interface Props { interface Props {
vote?: number vote?: number
@@ -36,13 +37,19 @@
}) })
;({ upvotes, downvotes } = res.comment_view.counts) ;({ upvotes, downvotes } = res.comment_view.counts)
} }
let voteRatio = $derived(
Math.floor(((upvotes ?? 0) / ((upvotes ?? 0) + (downvotes ?? 0))) * 100),
)
</script> </script>
<div <div
class={[ class={[
'h-full flex items-center overflow-hidden rounded-full hover:bg-transparent font-medium hover:dark:bg-transparent', 'h-full relative flex items-center overflow-hidden rounded-full hover:bg-transparent font-medium hover:dark:bg-transparent',
buttonColor.ghost, buttonColor.ghost,
voteRatio < 85 && settings.voteRatioBar && 'vote-ratio',
]} ]}
style="--vote-ratio: {voteRatio}%;"
> >
<button <button
onclick={() => castVote(vote == 1 ? 0 : 1)} onclick={() => castVote(vote == 1 ? 0 : 1)}
@@ -92,3 +99,20 @@
</span> </span>
</button> </button>
</div> </div>
<style>
.vote-ratio::before {
content: '';
position: absolute;
height: 1px;
width: 100%;
left: 0;
bottom: 0px;
z-index: 10;
background: linear-gradient(
to right,
var(--color-indigo-500) calc(var(--vote-ratio) - 5%),
var(--color-red-500) calc(var(--vote-ratio) + 5%)
);
}
</style>
+24 -1
View File
@@ -22,6 +22,7 @@
import { site } from '$lib/lemmy.svelte.js' import { site } from '$lib/lemmy.svelte.js'
import { vote as voteItem } from '$lib/lemmy/contentview.js' import { vote as voteItem } from '$lib/lemmy/contentview.js'
import { errorMessage } from '$lib/lemmy/error' import { errorMessage } from '$lib/lemmy/error'
import { settings } from '$lib/settings.svelte'
import type { Post } from 'lemmy-js-client' import type { Post } from 'lemmy-js-client'
import { buttonColor, toast } from 'mono-svelte' import { buttonColor, toast } from 'mono-svelte'
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons' import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
@@ -67,16 +68,21 @@
</script> </script>
{#if children}{@render children({ vote, score })}{:else} {#if children}{@render children({ vote, score })}{:else}
{@const voteRatio = Math.floor(
((upvotes ?? 0) / ((upvotes ?? 0) + (downvotes ?? 0))) * 100,
)}
<div <div
class={[ class={[
buttonColor.ghost, buttonColor.ghost,
'rounded-full h-full font-medium flex items-center *:p-2', 'rounded-full h-full font-medium flex items-center *:p-2 relative',
'hover:bg-white dark:hover:bg-zinc-900 overflow-hidden transition-colors shrink-0', 'hover:bg-white dark:hover:bg-zinc-900 overflow-hidden transition-colors shrink-0',
'text-inherit! divide-x divide-slate-200 dark:divide-zinc-800 shadow-xs', 'text-inherit! divide-x divide-slate-200 dark:divide-zinc-800 shadow-xs',
loading && 'animate-pulse opacity-75 pointer-events-none', loading && 'animate-pulse opacity-75 pointer-events-none',
voteRatio < 85 && settings.voteRatioBar && 'vote-ratio',
]} ]}
role="group" role="group"
aria-label={$t('aria.vote.group')} aria-label={$t('aria.vote.group')}
style="--vote-ratio: {voteRatio}%;"
> >
<button <button
onclick={() => castVote(vote == 1 ? 0 : 1)} onclick={() => castVote(vote == 1 ? 0 : 1)}
@@ -136,3 +142,20 @@
{/if} {/if}
</div> </div>
{/if} {/if}
<style>
.vote-ratio::before {
content: '';
position: absolute;
height: 1px;
width: 100%;
left: 0;
bottom: 0px;
z-index: 10;
background: linear-gradient(
to right,
var(--color-indigo-500) calc(var(--vote-ratio) - 5%),
var(--color-red-500) calc(var(--vote-ratio) + 5%)
);
}
</style>
+4
View File
@@ -850,6 +850,10 @@
"titleOpensUrl": { "titleOpensUrl": {
"title": "Title opens URL", "title": "Title opens URL",
"description": "Makes clicking the title open the post's attached URL, rather than the comments." "description": "Makes clicking the title open the post's attached URL, rather than the comments."
},
"voteRatioBar": {
"title": "Vote ratio bar",
"description": "When the controversiality of a post reaches greater than 15%, a bar will be displayed in the vote buttons to help visualize sentiment."
} }
}, },
"embeds": { "embeds": {
+2
View File
@@ -104,6 +104,7 @@ interface Settings {
messages: { messages: {
fullMarkdown: boolean fullMarkdown: boolean
} }
voteRatioBar: boolean
} }
export const defaultSettings: Settings = { export const defaultSettings: Settings = {
@@ -184,6 +185,7 @@ export const defaultSettings: Settings = {
messages: { messages: {
fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false, fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false,
}, },
voteRatioBar: true,
} }
function createSettingsState(initial: Settings): Settings { function createSettingsState(initial: Settings): Settings {
+7
View File
@@ -29,6 +29,7 @@
ArrowRight, ArrowRight,
ArrowsPointingOut, ArrowsPointingOut,
ArrowsRightLeft, ArrowsRightLeft,
ArrowsUpDown,
ArrowTopRightOnSquare, ArrowTopRightOnSquare,
ArrowTrendingDown, ArrowTrendingDown,
ArrowUpTray, ArrowUpTray,
@@ -402,6 +403,12 @@
title={$t('settings.app.absoluteDates.title')} title={$t('settings.app.absoluteDates.title')}
description={$t('settings.app.absoluteDates.description')} description={$t('settings.app.absoluteDates.description')}
/> />
<ToggleSetting
icon={ArrowsUpDown}
bind:checked={settings.voteRatioBar}
title={$t('settings.app.voteRatioBar.title')}
description={$t('settings.app.voteRatioBar.description')}
/>
<ToggleSetting <ToggleSetting
icon={CubeTransparent} icon={CubeTransparent}
supportedPlatforms={{ desktop: false, tablet: false, mobile: true }} supportedPlatforms={{ desktop: false, tablet: false, mobile: true }}