|
| 1 | +import { createComponent, Shade } from '@furystack/shades' |
| 2 | +import type { Roles } from 'common' |
| 3 | +import { getRoleDefinition } from 'common' |
| 4 | + |
| 5 | +type RoleTagProps = { |
| 6 | + roleName: Roles[number] |
| 7 | + variant: 'default' | 'added' | 'removed' |
| 8 | + onRemove?: () => void |
| 9 | + onRestore?: () => void |
| 10 | +} |
| 11 | + |
| 12 | +export const RoleTag = Shade<RoleTagProps>({ |
| 13 | + shadowDomName: 'role-tag', |
| 14 | + render: ({ props }) => { |
| 15 | + const role = getRoleDefinition(props.roleName) |
| 16 | + const baseStyle: Partial<CSSStyleDeclaration> = { |
| 17 | + display: 'inline-flex', |
| 18 | + alignItems: 'center', |
| 19 | + gap: '6px', |
| 20 | + padding: '4px 12px', |
| 21 | + borderRadius: '16px', |
| 22 | + fontSize: '13px', |
| 23 | + fontWeight: '500', |
| 24 | + transition: 'all 0.2s ease', |
| 25 | + } |
| 26 | + |
| 27 | + const variantStyles: Record<RoleTagProps['variant'], Partial<CSSStyleDeclaration>> = { |
| 28 | + default: { |
| 29 | + backgroundColor: 'var(--theme-background-paper)', |
| 30 | + border: '1px solid var(--theme-border-default)', |
| 31 | + color: 'var(--theme-text-primary)', |
| 32 | + }, |
| 33 | + added: { |
| 34 | + backgroundColor: 'rgba(76, 175, 80, 0.15)', |
| 35 | + border: '1px solid var(--theme-success-main, #4caf50)', |
| 36 | + color: 'var(--theme-success-dark, #2e7d32)', |
| 37 | + }, |
| 38 | + removed: { |
| 39 | + backgroundColor: 'rgba(244, 67, 54, 0.15)', |
| 40 | + border: '1px solid var(--theme-error-main, #f44336)', |
| 41 | + color: 'var(--theme-error-dark, #c62828)', |
| 42 | + textDecoration: 'line-through', |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + const buttonBaseStyle: Partial<CSSStyleDeclaration> = { |
| 47 | + background: 'none', |
| 48 | + border: 'none', |
| 49 | + cursor: 'pointer', |
| 50 | + padding: '0', |
| 51 | + margin: '0', |
| 52 | + marginLeft: '4px', |
| 53 | + fontSize: '14px', |
| 54 | + lineHeight: '1', |
| 55 | + opacity: '0.7', |
| 56 | + transition: 'opacity 0.2s ease', |
| 57 | + } |
| 58 | + |
| 59 | + const style = { ...baseStyle, ...variantStyles[props.variant] } |
| 60 | + |
| 61 | + return ( |
| 62 | + <span style={style} title={role.description}> |
| 63 | + {role.displayName} |
| 64 | + {props.variant === 'removed' && props.onRestore && ( |
| 65 | + <button |
| 66 | + type="button" |
| 67 | + onclick={(e) => { |
| 68 | + e.stopPropagation() |
| 69 | + props.onRestore?.() |
| 70 | + }} |
| 71 | + title="Restore role" |
| 72 | + style={{ |
| 73 | + ...buttonBaseStyle, |
| 74 | + color: 'var(--theme-error-dark, #c62828)', |
| 75 | + }} |
| 76 | + onmouseenter={(e) => { |
| 77 | + ;(e.target as HTMLButtonElement).style.opacity = '1' |
| 78 | + }} |
| 79 | + onmouseleave={(e) => { |
| 80 | + ;(e.target as HTMLButtonElement).style.opacity = '0.7' |
| 81 | + }} |
| 82 | + > |
| 83 | + ↩ |
| 84 | + </button> |
| 85 | + )} |
| 86 | + {props.variant !== 'removed' && props.onRemove && ( |
| 87 | + <button |
| 88 | + type="button" |
| 89 | + onclick={(e) => { |
| 90 | + e.stopPropagation() |
| 91 | + props.onRemove?.() |
| 92 | + }} |
| 93 | + title="Remove role" |
| 94 | + style={{ |
| 95 | + ...buttonBaseStyle, |
| 96 | + color: props.variant === 'added' ? 'var(--theme-success-dark, #2e7d32)' : 'var(--theme-text-secondary)', |
| 97 | + }} |
| 98 | + onmouseenter={(e) => { |
| 99 | + ;(e.target as HTMLButtonElement).style.opacity = '1' |
| 100 | + }} |
| 101 | + onmouseleave={(e) => { |
| 102 | + ;(e.target as HTMLButtonElement).style.opacity = '0.7' |
| 103 | + }} |
| 104 | + > |
| 105 | + × |
| 106 | + </button> |
| 107 | + )} |
| 108 | + </span> |
| 109 | + ) |
| 110 | + }, |
| 111 | +}) |
0 commit comments