|
| 1 | +<template> |
| 2 | + |
| 3 | + <div class="picture-password-option"> |
| 4 | + <!-- |
| 5 | + The label wraps the hidden checkbox and visible content, making the entire |
| 6 | + option region clickable/activatable while keeping a single focusable element. |
| 7 | + --> |
| 8 | + <label |
| 9 | + class="option-label" |
| 10 | + :class="[ |
| 11 | + $computedClass(optionLabelStyles), |
| 12 | + $computedClass({ |
| 13 | + ':focus-within': $coreOutline, |
| 14 | + }), |
| 15 | + ]" |
| 16 | + > |
| 17 | + <!-- |
| 18 | + Native checkbox: visually hidden but keyboard-focusable. |
| 19 | + We do not set the `disabled` HTML attribute so the input keeps focus. |
| 20 | + `aria-disabled` communicates the disabled state to assistive technology. |
| 21 | + --> |
| 22 | + <input |
| 23 | + type="checkbox" |
| 24 | + class="visuallyhidden" |
| 25 | + :checked="isSelected" |
| 26 | + :aria-disabled="disabled ? 'true' : undefined" |
| 27 | + @click.prevent="handleClick" |
| 28 | + @keydown="handleKeydown" |
| 29 | + > |
| 30 | + |
| 31 | + <div |
| 32 | + class="icon-container" |
| 33 | + :style="iconContainerStyle" |
| 34 | + > |
| 35 | + <span |
| 36 | + v-if="isSelected" |
| 37 | + class="badge" |
| 38 | + aria-hidden="true" |
| 39 | + data-testid="badge" |
| 40 | + :style="{ backgroundColor: $themeTokens.primary, color: $themeTokens.textInverted }" |
| 41 | + > |
| 42 | + {{ sequencePosition }} |
| 43 | + </span> |
| 44 | + |
| 45 | + <KIcon |
| 46 | + class="option-icon" |
| 47 | + :icon="icon" |
| 48 | + /> |
| 49 | + |
| 50 | + <span class="icon-label"> |
| 51 | + {{ labelText }} |
| 52 | + </span> |
| 53 | + </div> |
| 54 | + </label> |
| 55 | + </div> |
| 56 | + |
| 57 | +</template> |
| 58 | + |
| 59 | + |
| 60 | +<script> |
| 61 | +
|
| 62 | + import { computed } from 'vue'; |
| 63 | + import { themeTokens, themePalette } from 'kolibri-design-system/lib/styles/theme'; |
| 64 | + import { picturePasswordStrings } from 'kolibri-common/strings/picturePasswordStrings'; |
| 65 | +
|
| 66 | + export default { |
| 67 | + name: 'PicturePasswordOption', |
| 68 | +
|
| 69 | + setup(props, { emit }) { |
| 70 | + const $themeTokens = themeTokens(); |
| 71 | + const $themePalette = themePalette(); |
| 72 | +
|
| 73 | + const isSelected = computed(() => props.sequencePosition !== null); |
| 74 | +
|
| 75 | + const optionLabelStyles = computed(() => { |
| 76 | + if (isSelected.value) { |
| 77 | + return { |
| 78 | + border: `4px solid ${$themeTokens.primary}`, |
| 79 | + backgroundColor: $themePalette.blue.v_100, |
| 80 | + cursor: 'pointer', |
| 81 | + color: $themeTokens.text, |
| 82 | + fontWeight: 600, |
| 83 | + }; |
| 84 | + } |
| 85 | + const unSelectedStyles = { |
| 86 | + border: `2px solid ${$themeTokens.fineLine}`, |
| 87 | + backgroundColor: $themePalette.grey.v_100, |
| 88 | + color: $themeTokens.annotation, |
| 89 | + }; |
| 90 | +
|
| 91 | + if (props.disabled) { |
| 92 | + return { |
| 93 | + ...unSelectedStyles, |
| 94 | + cursor: 'not-allowed', |
| 95 | + }; |
| 96 | + } |
| 97 | + return { |
| 98 | + ...unSelectedStyles, |
| 99 | + cursor: 'pointer', |
| 100 | + ':hover': { |
| 101 | + borderColor: $themeTokens.primary, |
| 102 | + backgroundColor: $themePalette.blue.v_100, |
| 103 | + }, |
| 104 | + }; |
| 105 | + }); |
| 106 | +
|
| 107 | + const labelText = computed(() => { |
| 108 | + const getter = picturePasswordStrings[`${props.iconName}$`]; |
| 109 | + return getter ? getter() : props.iconName; |
| 110 | + }); |
| 111 | +
|
| 112 | + const iconContainerStyle = computed(() => { |
| 113 | + if (!props.disabled) return {}; |
| 114 | + return { filter: 'grayscale(100%)', opacity: '0.4' }; |
| 115 | + }); |
| 116 | +
|
| 117 | + const onSelect = () => { |
| 118 | + if (props.disabled) { |
| 119 | + emit('disabledSelect'); |
| 120 | + } else { |
| 121 | + emit('select', props.icon); |
| 122 | + } |
| 123 | + }; |
| 124 | + const handleClick = e => { |
| 125 | + e.preventDefault(); |
| 126 | + onSelect(); |
| 127 | + }; |
| 128 | +
|
| 129 | + const handleKeydown = e => { |
| 130 | + if (e.key === 'Enter') { |
| 131 | + e.preventDefault(); |
| 132 | + onSelect(); |
| 133 | + } |
| 134 | + }; |
| 135 | +
|
| 136 | + return { |
| 137 | + isSelected, |
| 138 | + optionLabelStyles, |
| 139 | + labelText, |
| 140 | + iconContainerStyle, |
| 141 | + handleClick, |
| 142 | + handleKeydown, |
| 143 | + }; |
| 144 | + }, |
| 145 | +
|
| 146 | + props: { |
| 147 | + /** |
| 148 | + * Resolved KIcon token to display (e.g. "birdColorful" or "birdStandard"). |
| 149 | + */ |
| 150 | + icon: { |
| 151 | + type: String, |
| 152 | + required: true, |
| 153 | + }, |
| 154 | + /** |
| 155 | + * Icon object name (e.g. "bird") used to look up the translated label. |
| 156 | + */ |
| 157 | + iconName: { |
| 158 | + type: String, |
| 159 | + required: true, |
| 160 | + }, |
| 161 | + /** |
| 162 | + * position in the selection sequence when this option is selected |
| 163 | + * or null when unselected. |
| 164 | + */ |
| 165 | + sequencePosition: { |
| 166 | + type: Number, |
| 167 | + default: null, |
| 168 | + validator: value => value === null || [1, 2, 3].includes(value), |
| 169 | + }, |
| 170 | + disabled: { |
| 171 | + type: Boolean, |
| 172 | + default: false, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }; |
| 176 | +
|
| 177 | +</script> |
| 178 | + |
| 179 | + |
| 180 | +<style lang="scss" scoped> |
| 181 | +
|
| 182 | + @import '~kolibri-design-system/lib/styles/definitions'; |
| 183 | +
|
| 184 | + $badge-size: 32px; |
| 185 | + $selected-border-width: 4px; |
| 186 | +
|
| 187 | + .picture-password-option { |
| 188 | + display: inline-flex; |
| 189 | + min-width: 44px; |
| 190 | + min-height: 44px; |
| 191 | + } |
| 192 | +
|
| 193 | + /* The label is the interactive surface */ |
| 194 | + .option-label { |
| 195 | + position: relative; |
| 196 | + display: flex; |
| 197 | + flex: 1; |
| 198 | + align-items: center; |
| 199 | + justify-content: center; |
| 200 | + padding: 12px; |
| 201 | + border-radius: 8px; |
| 202 | + transition: |
| 203 | + border-color $core-time, |
| 204 | + background-color $core-time; |
| 205 | + } |
| 206 | +
|
| 207 | + .icon-container { |
| 208 | + display: flex; |
| 209 | + flex-direction: column; |
| 210 | + gap: 4px; |
| 211 | + align-items: center; |
| 212 | + justify-content: center; |
| 213 | + } |
| 214 | +
|
| 215 | + .badge { |
| 216 | + position: absolute; |
| 217 | + // Position the badge centered on the top-left corner of the option, just outside the border. |
| 218 | + top: #{-1 * ($badge-size / 2 + $selected-border-width / 2)}; |
| 219 | + left: #{-1 * ($badge-size / 2 + $selected-border-width / 2)}; |
| 220 | + display: flex; |
| 221 | + align-items: center; |
| 222 | + justify-content: center; |
| 223 | + width: $badge-size; |
| 224 | + height: $badge-size; |
| 225 | + font-size: 16px; |
| 226 | + font-weight: bold; |
| 227 | + border-radius: 50%; |
| 228 | + } |
| 229 | +
|
| 230 | + .option-icon { |
| 231 | + width: 52px; |
| 232 | + height: 52px; |
| 233 | + } |
| 234 | +
|
| 235 | + .icon-label { |
| 236 | + font-size: 14px; |
| 237 | + text-align: center; |
| 238 | + } |
| 239 | +
|
| 240 | +</style> |
0 commit comments