akkoma-fe/src/components/color_input/color_input.vue

120 lines
2.8 KiB
Vue
Raw Normal View History

2018-10-07 16:59:22 +00:00
<template>
2019-07-05 07:17:44 +00:00
<div
class="color-input style-control"
2019-07-05 07:17:44 +00:00
:class="{ disabled: !present || disabled }"
>
<label
:for="name"
class="label"
2018-10-07 16:59:22 +00:00
>
2019-07-05 07:17:44 +00:00
{{ label }}
</label>
<Checkbox
2019-12-28 13:55:42 +00:00
v-if="typeof fallback !== 'undefined' && showOptionalTickbox"
2022-03-29 12:35:18 +00:00
:model-value="present"
:disabled="disabled"
class="opt"
2022-03-27 11:20:55 +00:00
@update:modelValue="$emit('update:modelValue', typeof modelValue === 'undefined' ? fallback : undefined)"
2019-07-05 07:17:44 +00:00
/>
<div class="input color-input-field">
<input
:id="name + '-t'"
class="textColor unstyled"
type="text"
2021-04-25 10:23:16 +00:00
:value="modelValue || fallback"
:disabled="!present || disabled"
2021-04-25 10:23:16 +00:00
@input="$emit('update:modelValue', $event.target.value)"
>
<input
v-if="validColor"
:id="name"
class="nativeColor unstyled"
type="color"
2021-04-25 10:23:16 +00:00
:value="modelValue || fallback"
:disabled="!present || disabled"
2021-04-25 10:23:16 +00:00
@input="$emit('update:modelValue', $event.target.value)"
>
<div
v-if="transparentColor"
class="transparentIndicator"
/>
<div
v-if="computedColor"
class="computedIndicator"
:style="{backgroundColor: fallback}"
/>
</div>
2019-07-05 07:17:44 +00:00
</div>
2018-10-07 16:59:22 +00:00
</template>
<style lang="scss" src="./color_input.scss"></style>
2018-10-07 16:59:22 +00:00
<script>
import Checkbox from '../checkbox/checkbox.vue'
import { hex2rgb } from '../../services/color_convert/color_convert.js'
2018-10-07 16:59:22 +00:00
export default {
2020-01-12 15:59:41 +00:00
components: {
Checkbox
},
2019-12-28 13:55:42 +00:00
props: {
// Name of color, used for identifying
name: {
required: true,
type: String
},
// Readable label
label: {
required: true,
type: String
},
// Color value, should be required but vue cannot tell the difference
// between "property missing" and "property set to undefined"
2021-04-25 10:23:16 +00:00
modelValue: {
2019-12-28 13:55:42 +00:00
required: false,
type: String,
default: undefined
},
// Color fallback to use when value is not defeind
fallback: {
required: false,
type: String,
default: undefined
},
// Disable the control
disabled: {
required: false,
type: Boolean,
default: false
},
// Show "optional" tickbox, for when value might become mandatory
showOptionalTickbox: {
required: false,
type: Boolean,
default: true
}
},
2022-03-27 11:20:55 +00:00
emits: ['update:modelValue'],
2018-10-07 16:59:22 +00:00
computed: {
present () {
2021-04-25 10:23:16 +00:00
return typeof this.modelValue !== 'undefined'
},
validColor () {
2021-04-25 10:23:16 +00:00
return hex2rgb(this.modelValue || this.fallback)
},
transparentColor () {
2021-04-25 10:23:16 +00:00
return this.modelValue === 'transparent'
},
computedColor () {
2021-04-25 10:23:16 +00:00
return this.modelValue && this.modelValue.startsWith('--')
2018-10-07 16:59:22 +00:00
}
}
}
</script>
<style lang="scss">
.color-control {
2018-11-20 19:14:49 +00:00
input.text-input {
2018-10-07 16:59:22 +00:00
max-width: 7em;
flex: 1;
}
}
</style>