Skip to content

Commit a8cb19d

Browse files
committed
fix: themesberg#1409 add iconFillColor and iconStrokeColor to Rating component
1 parent cecd92b commit a8cb19d

3 files changed

Lines changed: 48 additions & 21 deletions

File tree

src/lib/carousel/Thumbnail.svelte

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<script lang="ts">
22
import type { HTMLImgAttributes } from 'svelte/elements';
33
import { twMerge } from 'tailwind-merge';
4+
interface $$Props extends HTMLImgAttributes {
5+
selected?: boolean;
6+
alt?: string;
7+
activeClass?: string;
8+
inactiveClass?: string;
9+
}
410
5-
export let selected: boolean = false;
6-
7-
export let activeClass = 'opacity-100';
8-
export let inactiveClass = 'opacity-60';
11+
export let selected: $$Props['selected'] = false;
12+
export let alt: $$Props['alt'] = '';
13+
export let activeClass: $$Props['activeClass'] = 'opacity-100';
14+
export let inactiveClass: $$Props['inactiveClass'] = 'opacity-60';
915
</script>
1016

11-
<img alt="..." {...$$restProps} class={twMerge(selected ? activeClass : inactiveClass, $$props.class)} />
17+
<img {...$$restProps} {alt} class={twMerge(selected ? activeClass : inactiveClass, $$props.class)} />
1218

1319
<!--
1420
@component

src/lib/rating/Rating.svelte

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,54 @@
33
import { twMerge } from 'tailwind-merge';
44
import generateId from '../utils/generateId.js';
55
import type { ComponentType } from 'svelte';
6+
import type { HTMLAttributes } from 'svelte/elements';
67
7-
export let divClass: string = 'flex items-center';
8-
export let size: number = 24;
9-
export let total: number = 5;
10-
export let rating: number = 4;
11-
export let partialId: string = 'partialStar' + generateId();
12-
export let icon: ComponentType = Star;
13-
export let count: boolean = false;
8+
interface $$Props extends HTMLAttributes<HTMLDivElement> {
9+
divClass?: string;
10+
size?: number;
11+
total?: number;
12+
rating: number;
13+
partialId?: string;
14+
icon?: ComponentType;
15+
count?: boolean;
16+
iconFillColor?: string;
17+
iconStrokeColor?: string;
18+
}
19+
20+
export let divClass: $$Props['divClass'] = 'flex items-center';
21+
export let size: $$Props['size'] = 24;
22+
export let total: NonNullable<$$Props['total']> = 5;
23+
export let rating: $$Props['rating'] = 4;
24+
export let partialId: $$Props['partialId'] = 'partialStar' + generateId();
25+
export let icon: $$Props['icon'] = Star;
26+
export let count: $$Props['count'] = false;
27+
export let iconFillColor: $$Props['iconFillColor'];
28+
export let iconStrokeColor: $$Props['iconStrokeColor'];
1429
1530
// generate unique id for full star and gray star
1631
const fullStarId: string = generateId();
1732
const grayStarId: string = generateId();
18-
let fullStars: number = Math.floor(rating);
19-
let rateDiffence = rating - fullStars;
20-
let percentRating = Math.round(rateDiffence * 100);
21-
let grayStars: number = total - (fullStars + Math.ceil(rateDiffence));
33+
$: fullStars = Math.floor(rating);
34+
$: rateDiffence = rating - fullStars;
35+
$: percentRating = Math.round(rateDiffence * 100);
36+
$: grayStars = total - (fullStars + Math.ceil(rateDiffence));
2237
// console.log(fullStars, grayStars, rateDiffence, percentRating)
2338
</script>
2439

2540
<div class={twMerge(divClass, $$props.class)}>
2641
{#if count}
27-
<svelte:component this={icon} fillPercent={100} {size} />
42+
<svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} fillPercent={100} {size} />
2843
<p class="ms-2 text-sm font-bold text-gray-900 dark:text-white">{rating}</p>
2944
<slot />
3045
{:else}
3146
{#each Array(fullStars) as star}
32-
<svelte:component this={icon} {size} fillPercent={100} id={fullStarId} />
47+
<svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} {size} fillPercent={100} id={fullStarId} />
3348
{/each}
3449
{#if percentRating}
35-
<svelte:component this={icon} {size} fillPercent={percentRating} id={partialId} />
50+
<svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} {size} fillPercent={percentRating} id={partialId} />
3651
{/if}
3752
{#each Array(grayStars) as star}
38-
<svelte:component this={icon} {size} fillPercent={0} id={grayStarId} />
53+
<svelte:component this={icon} fillColor={iconFillColor} strokeColor={iconStrokeColor} {size} fillPercent={0} id={grayStarId} />
3954
{/each}
4055
{#if $$slots.text}
4156
<slot name="text" />

src/routes/docs/components/rating.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ The default rating icon is a star. Set the total and rating props. The `id` prop
3939
4040
<Rating id="example-1" total={5} size={50} rating={1.4} />
4141
<Rating id="example-1b" total={5} size={50} rating={4.66} />
42+
<Rating id="example-1b" iconFillColor='#008800' iconStrokeColor='#008800' total={5} size={50} rating={4.66} />
4243
```
4344

4445
## Stars
4546

4647
You can use the Star component with the `id` and `fillPercent` props.
4748

48-
```svelte example
49+
```svelte example class="flex"
4950
<script>
5051
import { Star } from 'flowbite-svelte';
5152
</script>
@@ -75,6 +76,9 @@ If you also want to show a text near the stars you can use the `text` slot to ad
7576
<Rating id="example-3" total={5} rating={3.4}>
7677
<p slot="text" class="ms-2 text-sm font-medium text-gray-500 dark:text-gray-400">3.4 out of 5</p>
7778
</Rating>
79+
<Rating id="example-3" total={5} rating={2.8} iconFillColor='#008800' iconStrokeColor='#008800'>
80+
<p slot="text" class="ms-2 text-sm font-medium text-gray-500 dark:text-gray-400">2.8 out of 5</p>
81+
</Rating>
7882
```
7983

8084
## Rating count
@@ -105,6 +109,7 @@ The default icon size is `24`. Import your icon and set it in a icon props.
105109
106110
<Rating total={5} rating={3.3} id="example-5" icon={Heart} />
107111
<Rating total={10} rating={7.6} id="example-5b" icon={Heart} />
112+
<Rating total={10} rating={7.6} id="example-5b" icon={Heart} iconFillColor='#3752d6' iconStrokeColor='#3752d6'/>
108113
```
109114

110115
```svelte example
@@ -114,6 +119,7 @@ The default icon size is `24`. Import your icon and set it in a icon props.
114119
115120
<Rating total={5} rating={4.7} id="example-5c" icon={Thumbup} />
116121
<Rating total={10} rating={8.2} id="example-5d" icon={Thumbup} />
122+
<Rating total={10} rating={8.2} id="example-5d" icon={Thumbup} iconFillColor='#ff3f00' iconStrokeColor='#ff3f00'/>
117123
```
118124

119125
## AdvancedRating component

0 commit comments

Comments
 (0)