vue#onUnmounted JavaScript Examples
The following examples show how to use
vue#onUnmounted.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useClipboard.js From ant-simple-pro with MIT License | 6 votes |
export function useClipboard() {
const text = ref('')
async function onCopy() {
text.value = await navigator.clipboard.readText()
}
onMounted(() => {
window.addEventListener('copy', onCopy)
})
onUnmounted(() => {
window.removeEventListener('copy', onCopy)
})
function write(txt) {
text.value = txt
return navigator.clipboard.writeText(txt)
}
return {
text,
write
}
}
Example #2
Source File: useDarkMode.js From website with MIT License | 5 votes |
export default function useDarkMode() {
const isDarkMode = ref(false);
const updateDarkModeClass = (value = isDarkMode.value) => {
// set `class="dark"` on `<html>` element
const htmlEl = window?.document.querySelector('html');
htmlEl?.classList.toggle('dark', value);
const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
if ((value && systemDarkMode) || (!value && !systemDarkMode)) {
localStorage.removeItem('guide-color-scheme');
} else if (value && !systemDarkMode) {
localStorage.setItem('guide-color-scheme', 'dark');
} else if (!value && systemDarkMode) {
localStorage.setItem('guide-color-scheme', 'light');
}
}
const mediaQuery = ref < MediaQueryList | null > (null)
const onMediaQueryChange = (event) => {
isDarkMode.value = event.matches;
};
onMounted(() => {
// get stored preference and `prefers-color-scheme` media query and set the initial mode
const userMode = localStorage.getItem('guide-color-scheme');
mediaQuery.value = window.matchMedia('(prefers-color-scheme: dark)');
isDarkMode.value = userMode === 'dark' || (userMode !== 'light' && mediaQuery.value.matches);
// watch changes
mediaQuery.value.addEventListener('change', onMediaQueryChange);
watch(isDarkMode, updateDarkModeClass, { immediate: true });
});
onUnmounted(() => {
mediaQuery.value?.removeEventListener('change', onMediaQueryChange);
});
return {
isDarkMode,
};
}
Example #3
Source File: useHotkeys.js From ant-simple-pro with MIT License | 5 votes |
export function useHotkeys(keys, callback, options, deps) {
if (Array.isArray(options)) {
deps = options || []
options = undefined
}
const elRef = ref()
const {
enableOnTags,
filter,
keyup,
keydown,
filterPreventDefault = true,
enabled = true,
enableOnContentEditable = false
} = options || {}
function keyHandler(keyboardEvent, hotkeysEvent) {
if (filter && !filter(keyboardEvent)) {
return !filterPreventDefault
}
// Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.
if (
(isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags)) ||
(keyboardEvent.target?.isContentEditable && !enableOnContentEditable)
) {
return true
}
if (!elRef.value || document.activeElement === elRef.value) {
callback(keyboardEvent, hotkeysEvent)
return true
}
return false
}
function setHotKeys() {
if (!enabled) {
return
}
// In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.
if (keyup && keydown !== true) {
options.keydown = false
}
hotkeys(keys, options || {}, keyHandler)
}
onMounted(() => {
setHotKeys()
})
onUnmounted(() => {
hotkeys.unbind(keys, keyHandler)
})
watch(deps ? deps.map(v => () => v) : [], () => { // eslint-disable-line
setHotKeys()
})
watch([() => options, () => keys, () => enabled], () => {
setHotKeys()
})
return elRef
}
Example #4
Source File: vue3-highcharts.js From vue3-highcharts with MIT License | 5 votes |
vueHighcharts = defineComponent({
name: 'VueHighchart',
props: {
type: {
type: String,
default: 'chart',
},
options: {
type: Object,
required: true,
},
redrawOnUpdate: {
type: Boolean,
default: true,
},
oneToOneUpdate: {
type: Boolean,
default: false,
},
animateOnUpdate: {
type: Boolean,
default: true,
},
},
setup(props, { emit }) {
const chartRef = ref(null);
const chart = ref(null);
const { options } = toRefs(props);
if (options.value && Highcharts[props.type]) {
watch(options, (newValue) => {
if (chart.value) {
chart.value.update(newValue, props.redrawOnUpdate, props.oneToOneOnUpdate, props.animateOnUpdate);
emit('updated');
}
}, { deep: true });
onMounted(() => {
chart.value = Highcharts[props.type](chartRef.value, options.value, () => {
emit('rendered');
});
});
onUnmounted(() => {
if (chart.value) chart.value.destroy();
emit('destroyed');
});
} else if (!props.options) {
console.warn('The "options" parameter is required.');
} else {
console.warn(`${props.type} is not a valid highcharts type or has not been imported`);
}
// Rather than returning a render function here. We'll return the chart ref and highcharts
// instance so there exposed.
return {
chartRef,
chart,
};
},
render() {
return h('div', {
class: 'vue-highcharts',
ref: 'chartRef',
});
},
})