Skip to content

Commit

Permalink
[BarStack] Support passing color for data item (along with `classes…
Browse files Browse the repository at this point in the history
…` and `style`)
  • Loading branch information
techniq committed Apr 12, 2024
1 parent 2a4f3f2 commit 35aa4e5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-countries-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-ux": patch
---

[BarStack] Support passing `color` for data item (along with `classes` and `style`)
2 changes: 2 additions & 0 deletions packages/svelte-ux/src/lib/components/BarStack.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
export let data: {
label: string;
value: number;
color?: string;
style?: string;
classes?: {
root?: string;
Expand Down Expand Up @@ -45,6 +46,7 @@
<slot {item} total={total ?? sum(data, (d) => d.value)}>
<div
class={cls('group-first:rounded-l group-last:rounded-r', item.classes?.bar)}
style:background-color={item.color}
style={item.style}
>
<slot name="bar" {item} total={total ?? sum(data, (d) => d.value)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
import { cls } from '$lib/utils/styles.js';
const data = [
{ label: 'Chrome', value: 65, classes: { bar: 'bg-warning' }, _style: 'background:red' },
{ label: 'Chrome', value: 65, classes: { bar: 'bg-warning' } },
{ label: 'Safari', value: 18.55, classes: { bar: 'bg-info' } },
{ label: 'Edge', value: 5.03, classes: { bar: 'bg-success' } },
{ label: 'Firefox', value: 2.8, classes: { bar: 'bg-danger' } },
];
const dataWithColorProp = [
{ label: 'Chrome', value: 65, color: 'yellow' },
{ label: 'Safari', value: 18.55, color: 'blue' },
{ label: 'Edge', value: 5.03, color: 'green' },
{ label: 'Firefox', value: 2.8, color: 'red' },
];
function randomDataGen() {
return data.map((d) => {
return {
Expand Down Expand Up @@ -44,6 +51,12 @@
<BarStack {data} class="gap-1" />
</Preview>

<h2>Color via prop</h2>

<Preview>
<BarStack data={dataWithColorProp} class="gap-1" />
</Preview>

<h2>Bar slot</h2>

<Preview>
Expand Down

0 comments on commit 35aa4e5

Please sign in to comment.