-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add capacity indicator component (#19)
+ Don't automatically open browser for Storybook + Update icons package
- Loading branch information
1 parent
ad992eb
commit 772fcb1
Showing
8 changed files
with
149 additions
and
13 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
echo "<script setup lang="ts"> | ||
echo "<script setup lang=\"ts\"> | ||
type Props = { | ||
}; | ||
|
17 changes: 17 additions & 0 deletions
17
src/components/CapacityIndicator/CapacityIndicator.spec.ts
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,17 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { AllInclusiveIcon } from "@bcc-code/icons-vue"; | ||
|
||
import { mount } from "@vue/test-utils"; | ||
import CapacityIndicator from "./CapacityIndicator.vue"; | ||
|
||
describe("CapacityIndicator", () => { | ||
it("shows the remaining capacity", () => { | ||
const wrapper = mount(CapacityIndicator, { props: { capacity: 20, used: 14 } }); | ||
expect(wrapper.text()).toBe("6"); | ||
}); | ||
|
||
it("shows an icon if the capacity is infinite", () => { | ||
const wrapper = mount(CapacityIndicator, { props: { capacity: Infinity, used: 14 } }); | ||
expect(wrapper.findComponent(AllInclusiveIcon).isVisible()).toBe(true); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/components/CapacityIndicator/CapacityIndicator.stories.ts
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,41 @@ | ||
import CapacityIndicator from "./CapacityIndicator.vue"; | ||
|
||
import type { Meta, StoryFn } from "@storybook/vue3"; | ||
|
||
export default { | ||
title: "Components/CapacityIndicator", | ||
component: CapacityIndicator, | ||
argTypes: {}, | ||
} as Meta<typeof CapacityIndicator>; | ||
|
||
const Template: StoryFn<typeof CapacityIndicator> = (args) => ({ | ||
components: { CapacityIndicator }, | ||
setup() { | ||
return { args }; | ||
}, | ||
template: ` | ||
<CapacityIndicator v-bind="args" /> | ||
`, | ||
}); | ||
|
||
export const Example = Template.bind({}); | ||
Example.parameters = { | ||
viewMode: "docs", | ||
}; | ||
Example.args = { | ||
capacity: 20, | ||
used: 14, | ||
}; | ||
|
||
export const State: StoryFn<typeof CapacityIndicator> = () => ({ | ||
components: { CapacityIndicator }, | ||
template: ` | ||
<div class="flex items-center space-x-4"> | ||
<CapacityIndicator :capacity="200" :used="1" /> | ||
<CapacityIndicator :capacity="20" :used="6" /> | ||
<CapacityIndicator :capacity="20" :used="18" /> | ||
<CapacityIndicator :capacity="20" :used="20" /> | ||
<CapacityIndicator :capacity="Infinity" /> | ||
</div> | ||
`, | ||
}); |
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,74 @@ | ||
<script setup lang="ts"> | ||
import { AllInclusiveIcon } from "@bcc-code/icons-vue"; | ||
import { ref } from "vue"; | ||
type Props = { | ||
capacity?: number; | ||
used?: number; | ||
}; | ||
const props = withDefaults(defineProps<Props>(), { | ||
capacity: Infinity, | ||
used: 0, | ||
}); | ||
const size = 40; | ||
const progress = ref(props.capacity === Infinity ? 100 : (props.used / props.capacity) * 100); | ||
const trackWidth = 2; | ||
const center = size / 2; | ||
const radius = center - trackWidth; | ||
const dashArray = 2 * Math.PI * radius; | ||
const dashOffset = dashArray * ((100 - progress.value) / 100); | ||
</script> | ||
|
||
<template> | ||
<div class="relative inline-block"> | ||
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2"> | ||
<span v-if="capacity === Infinity"> | ||
<AllInclusiveIcon class="h-4 w-4 text-neutral-900" /> | ||
</span> | ||
<span | ||
v-else | ||
class="text-sm font-bold" | ||
:class="{ | ||
'text-neutral-900': progress < 50, | ||
'text-muddy-waters-600': progress >= 50 && progress < 100, | ||
'text-red-900': progress >= 100, | ||
}" | ||
> | ||
{{ capacity - used }} | ||
</span> | ||
</div> | ||
|
||
<svg class="-rotate-90" :style="{ width: size, height: size }"> | ||
<circle | ||
:cx="center" | ||
:cy="center" | ||
:r="radius" | ||
:stroke-width="trackWidth" | ||
fill="transparent" | ||
:class="{ | ||
'stroke-neutral-200': capacity === Infinity, | ||
'stroke-neutral-300': capacity !== Infinity && progress < 50, | ||
'stroke-muddy-waters-100': capacity !== Infinity && progress >= 50, | ||
}" | ||
/> | ||
<circle | ||
v-if="capacity !== Infinity" | ||
:cx="center" | ||
:cy="center" | ||
:r="radius" | ||
:stroke-width="trackWidth" | ||
:stroke-dasharray="dashArray" | ||
:stroke-dashoffset="dashOffset" | ||
fill="transparent" | ||
:class="{ | ||
'stroke-neutral-500': progress < 50, | ||
'stroke-muddy-waters-500': progress >= 50 && progress < 100, | ||
'stroke-red-900': progress >= 100, | ||
}" | ||
/> | ||
</svg> | ||
</div> | ||
</template> |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as BccButton } from "./components/BccButton/BccButton.vue"; | ||
export { default as BccInput } from "./components/BccInput/BccInput.vue"; | ||
export { default as Badge } from "./components/Badge/Badge.vue"; | ||
export { default as CapacityIndicator } from "./components/CapacityIndicator/CapacityIndicator.vue"; |