diff options
Diffstat (limited to 'app/assets/javascripts/vue_shared/components')
-rw-r--r-- | app/assets/javascripts/vue_shared/components/local_storage_sync.vue | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/assets/javascripts/vue_shared/components/local_storage_sync.vue b/app/assets/javascripts/vue_shared/components/local_storage_sync.vue new file mode 100644 index 00000000000..b5d6b872547 --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/local_storage_sync.vue @@ -0,0 +1,39 @@ +<script> +export default { + props: { + storageKey: { + type: String, + required: true, + }, + value: { + type: String, + required: false, + default: '', + }, + }, + watch: { + value(newVal) { + this.saveValue(newVal); + }, + }, + mounted() { + // On mount, trigger update if we actually have a localStorageValue + const value = this.getValue(); + + if (value && this.value !== value) { + this.$emit('input', value); + } + }, + methods: { + getValue() { + return localStorage.getItem(this.storageKey); + }, + saveValue(val) { + localStorage.setItem(this.storageKey, val); + }, + }, + render() { + return this.$slots.default; + }, +}; +</script> |