forked from qwikifiers/qwik-storefront-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce9ff11
commit 339ebfe
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/qwik-storefront-ui/src/components/SfBadge/SfBadge.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,38 @@ | ||
import { component$ } from '@builder.io/qwik'; | ||
import { SfBadgePlacement, SfBadgeProps } from './types'; | ||
|
||
export const SfBadge = component$<SfBadgeProps>( | ||
({ | ||
content, | ||
variant, | ||
max = 99, | ||
placement = SfBadgePlacement['top-right'], | ||
...attributes | ||
}: SfBadgeProps) => { | ||
const isDot = variant === 'dot'; | ||
let displayValue = content; | ||
if (isDot) { | ||
displayValue = ''; | ||
} else if (!Number.isNaN(content) && Number(content) > max) { | ||
displayValue = `${max}+`; | ||
} | ||
return ( | ||
<span | ||
class={[ | ||
'block absolute py-0.5 px-1 bg-secondary-700 font-medium text-white text-[8px] leading-[8px] rounded-xl', | ||
{ | ||
'min-w-[12px] min-h-[12px]': !isDot, | ||
'w-[10px] h-[10px]': isDot, | ||
'top-0 right-0 -translate-x-0.5 translate-y-0.5': placement === 'top-right', | ||
'top-0 left-0 translate-x-0.5 translate-y-0.5': placement === 'top-left', | ||
'bottom-0 right-0 -translate-x-0.5 -translate-y-0.5': placement === 'bottom-right', | ||
'bottom-0 left-0 translate-x-0.5 -translate-y-0.5': placement === 'bottom-left', | ||
}, | ||
]} | ||
data-testid="badge" | ||
{...attributes} | ||
> | ||
{displayValue} | ||
</span> | ||
); | ||
}) |
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,3 @@ | ||
export * from './types'; | ||
|
||
export { SfBadge } from './SfBadge'; |
18 changes: 18 additions & 0 deletions
18
packages/qwik-storefront-ui/src/components/SfBadge/types.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,18 @@ | ||
export type SfBadgeProps = { | ||
content?: string | number; | ||
max?: number; | ||
placement?: `${SfBadgePlacement}`; | ||
variant?: `${SfBadgeVariant}`; | ||
} | ||
|
||
export enum SfBadgeVariant { | ||
standard = 'standard', | ||
dot = 'dot', | ||
} | ||
|
||
export enum SfBadgePlacement { | ||
'top-right' = 'top-right', | ||
'top-left' = 'top-left', | ||
'bottom-right' = 'bottom-right', | ||
'bottom-left' = 'bottom-left', | ||
} |
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