aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/src/components/admin/ColorPickerField.vue
blob: 4ec6d47fef62cd298724fde0c0b1ff0ff8a21c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!--
  - SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
	<div class="field">
		<label :for="id">{{ displayName }}</label>
		<div class="field__row">
			<NcColorPicker :value.sync="localValue"
				:advanced-fields="true"
				@update:value="debounceSave">
				<NcButton :id="id"
					class="field__button"
					type="primary"
					:aria-label="t('theming', 'Select a custom color')"
					data-admin-theming-setting-color-picker>
					<template #icon>
						<NcLoadingIcon v-if="loading"
							:appearance="calculatedTextColor === '#ffffff' ? 'light' : 'dark'"
							:size="20" />
						<Palette v-else :size="20" />
					</template>
					{{ value }}
				</NcButton>
			</NcColorPicker>
			<div class="field__color-preview" data-admin-theming-setting-color />
			<NcButton v-if="value !== defaultValue"
				type="tertiary"
				:aria-label="t('theming', 'Reset to default')"
				data-admin-theming-setting-color-reset
				@click="undo">
				<template #icon>
					<Undo :size="20" />
				</template>
			</NcButton>
		</div>
		<div v-if="description" class="description">
			{{ description }}
		</div>

		<NcNoteCard v-if="errorMessage"
			type="error"
			:show-alert="true">
			<p>{{ errorMessage }}</p>
		</NcNoteCard>
	</div>
</template>

<script>
import { colord } from 'colord'
import debounce from 'debounce'

import NcButton from '@nextcloud/vue/components/NcButton'
import NcColorPicker from '@nextcloud/vue/components/NcColorPicker'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
import Undo from 'vue-material-design-icons/UndoVariant.vue'
import Palette from 'vue-material-design-icons/Palette.vue'

import TextValueMixin from '../../mixins/admin/TextValueMixin.js'

export default {
	name: 'ColorPickerField',

	components: {
		NcButton,
		NcColorPicker,
		NcLoadingIcon,
		NcNoteCard,
		Undo,
		Palette,
	},

	mixins: [
		TextValueMixin,
	],

	props: {
		name: {
			type: String,
			required: true,
		},
		description: {
			type: String,
			default: '',
		},
		value: {
			type: String,
			required: true,
		},
		textColor: {
			type: String,
			default: null,
		},
		defaultValue: {
			type: String,
			required: true,
		},
		displayName: {
			type: String,
			required: true,
		},
	},

	emits: ['update:theming'],

	data() {
		return {
			loading: false,
		}
	},

	computed: {
		calculatedTextColor() {
			const color = colord(this.value)
			return color.isLight() ? '#000000' : '#ffffff'
		},
		usedTextColor() {
			if (this.textColor) {
				return this.textColor
			}
			return this.calculatedTextColor
		},
	},

	methods: {
		debounceSave: debounce(async function() {
			this.loading = true
			await this.save()
			this.$emit('update:theming')
			this.loading = false
		}, 200),
	},
}
</script>

<style lang="scss" scoped>
@use './shared/field' as *;

.description {
	color: var(--color-text-maxcontrast);
}

.field {
	&__button {
		background-color: v-bind('value') !important;
		color: v-bind('usedTextColor') !important;
	}

	&__color-preview {
		width: var(--default-clickable-area);
		border-radius: var(--border-radius-large);
		background-color: v-bind('value');
	}
}
</style>