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

120 lines
2.8 KiB
Vue
Raw Normal View History

2018-10-07 18:59:22 +02:00
<template>
2019-07-05 09:17:44 +02:00
<div
class="color-input style-control"
2019-07-05 09:17:44 +02:00
:class="{ disabled: !present || disabled }"
>
<label
:for="name"
class="label"
2018-10-07 18:59:22 +02:00
>
2019-07-05 09:17:44 +02:00
{{ label }}
</label>
<Checkbox
2019-12-28 14:55:42 +01:00
v-if="typeof fallback !== 'undefined' && showOptionalTickbox"
2022-03-29 14:35:18 +02:00
:model-value="present"
:disabled="disabled"
class="opt"
2022-03-27 13:20:55 +02:00
@update:modelValue="$emit('update:modelValue', typeof modelValue === 'undefined' ? fallback : undefined)"
2019-07-05 09:17:44 +02:00
/>
<div class="input color-input-field">
<input
:id="name + '-t'"
class="textColor unstyled"
type="text"
2021-04-25 12:23:16 +02:00
:value="modelValue || fallback"
:disabled="!present || disabled"
2021-04-25 12:23:16 +02:00
@input="$emit('update:modelValue', $event.target.value)"
>
<input
v-if="validColor"
:id="name"
class="nativeColor unstyled"
type="color"
2021-04-25 12:23:16 +02:00
:value="modelValue || fallback"
:disabled="!present || disabled"
2021-04-25 12:23:16 +02: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 09:17:44 +02:00
</div>
2018-10-07 18:59:22 +02:00
</template>
<script>
import Checkbox from '../checkbox/checkbox.vue'
import { hex2rgb } from '../../services/color_convert/color_convert.js'
2018-10-07 18:59:22 +02:00
export default {
2020-01-12 16:59:41 +01:00
components: {
Checkbox
},
2019-12-28 14:55:42 +01: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 12:23:16 +02:00
modelValue: {
2019-12-28 14:55:42 +01: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 13:20:55 +02:00
emits: ['update:modelValue'],
2018-10-07 18:59:22 +02:00
computed: {
present () {
2021-04-25 12:23:16 +02:00
return typeof this.modelValue !== 'undefined'
},
validColor () {
2021-04-25 12:23:16 +02:00
return hex2rgb(this.modelValue || this.fallback)
},
transparentColor () {
2021-04-25 12:23:16 +02:00
return this.modelValue === 'transparent'
},
computedColor () {
2021-04-25 12:23:16 +02:00
return this.modelValue && this.modelValue.startsWith('--')
2018-10-07 18:59:22 +02:00
}
}
}
</script>
2022-07-31 11:35:48 +02:00
<style lang="scss" src="./color_input.scss"></style>
2018-10-07 18:59:22 +02:00
<style lang="scss">
.color-control {
2018-11-20 20:14:49 +01:00
input.text-input {
2018-10-07 18:59:22 +02:00
max-width: 7em;
flex: 1;
}
}
</style>