mirror of
https://github.com/tenrok/OverlayScrollbars.git
synced 2026-06-19 23:50:36 +03:00
improve code
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
"overlayscrollbars",
|
"overlayscrollbars",
|
||||||
"react",
|
"react",
|
||||||
"component",
|
"component",
|
||||||
|
"hook",
|
||||||
"hooks",
|
"hooks",
|
||||||
"styleable",
|
"styleable",
|
||||||
"scrollbar",
|
"scrollbar",
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 0.4.0
|
||||||
|
|
||||||
|
The component was created.
|
||||||
|
Depends on `OverlayScrollbars` version `^2.0.0` and `Svelte` version `^3.44.0`.
|
||||||
@@ -115,9 +115,26 @@ import { useOverlayScrollbars } from "overlayscrollbars-vue";
|
|||||||
// example usage
|
// example usage
|
||||||
const Component = {
|
const Component = {
|
||||||
setup() {
|
setup() {
|
||||||
|
const div = ref(null);
|
||||||
const reactiveParams = reactive({ options, events });
|
const reactiveParams = reactive({ options, events });
|
||||||
const [initialize, instance] = useOverlayScrollbars(reactiveParams);
|
const [initialize, instance] = useOverlayScrollbars(reactiveParams);
|
||||||
const div = ref(null);
|
|
||||||
|
/**
|
||||||
|
* or:
|
||||||
|
*
|
||||||
|
* const params = ref<{ options?: PartialOptions; events?: EventListeners } | undefined>();
|
||||||
|
* const [initialize, instance] = useOverlayScrollbars(params);
|
||||||
|
*
|
||||||
|
* or:
|
||||||
|
*
|
||||||
|
* const options = ref<PartialOptions | undefined>();
|
||||||
|
* const events = ref<EventListeners | undefined>();
|
||||||
|
* const [initialize, instance] = useOverlayScrollbars({
|
||||||
|
* options,
|
||||||
|
* events,
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
initialize({ target: div.value });
|
initialize({ target: div.value });
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
import { shallowRef, unref, watch } from 'vue';
|
import { shallowRef, unref, watch } from 'vue';
|
||||||
import { OverlayScrollbars } from 'overlayscrollbars';
|
import { OverlayScrollbars } from 'overlayscrollbars';
|
||||||
import type { Ref } from 'vue';
|
import type { Ref, UnwrapRef } from 'vue';
|
||||||
import type { InitializationTarget } from 'overlayscrollbars';
|
import type { InitializationTarget } from 'overlayscrollbars';
|
||||||
import type {
|
import type {
|
||||||
OverlayScrollbarsComponentProps,
|
OverlayScrollbarsComponentProps,
|
||||||
OverlayScrollbarsComponentRef,
|
OverlayScrollbarsComponentRef,
|
||||||
} from './OverlayScrollbarsComponent.types';
|
} from './OverlayScrollbarsComponent.types';
|
||||||
|
|
||||||
type Options = OverlayScrollbarsComponentProps['options'];
|
|
||||||
type Events = OverlayScrollbarsComponentProps['events'];
|
|
||||||
|
|
||||||
export interface UseOverlayScrollbarsParams {
|
export interface UseOverlayScrollbarsParams {
|
||||||
/** OverlayScrollbars options. */
|
/** OverlayScrollbars options. */
|
||||||
options?: Options | Ref<Options | undefined>;
|
options?:
|
||||||
|
| OverlayScrollbarsComponentProps['options']
|
||||||
|
| Ref<OverlayScrollbarsComponentProps['options']>;
|
||||||
/** OverlayScrollbars events. */
|
/** OverlayScrollbars events. */
|
||||||
events?: Events | Ref<Events | undefined>;
|
events?:
|
||||||
|
| OverlayScrollbarsComponentProps['events']
|
||||||
|
| Ref<OverlayScrollbarsComponentProps['events']>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UseOverlayScrollbarsInitialization = (
|
export type UseOverlayScrollbarsInitialization = (
|
||||||
@@ -33,25 +34,17 @@ export type UseOverlayScrollbarsInstance = () => ReturnType<
|
|||||||
* The second value is a function which returns the current OverlayScrollbars instance or `null` if not initialized.
|
* The second value is a function which returns the current OverlayScrollbars instance or `null` if not initialized.
|
||||||
*/
|
*/
|
||||||
export const useOverlayScrollbars = (
|
export const useOverlayScrollbars = (
|
||||||
params?: UseOverlayScrollbarsParams | Ref<UseOverlayScrollbarsParams>
|
params?: UseOverlayScrollbarsParams | Ref<UseOverlayScrollbarsParams | undefined>
|
||||||
): [UseOverlayScrollbarsInitialization, UseOverlayScrollbarsInstance] => {
|
): [UseOverlayScrollbarsInitialization, UseOverlayScrollbarsInstance] => {
|
||||||
|
let instance: ReturnType<UseOverlayScrollbarsInstance> = null;
|
||||||
|
let options: UnwrapRef<UseOverlayScrollbarsParams['options']>;
|
||||||
|
let events: UnwrapRef<UseOverlayScrollbarsParams['events']>;
|
||||||
const paramsRef = shallowRef(params || {});
|
const paramsRef = shallowRef(params || {});
|
||||||
const variables = shallowRef<{
|
|
||||||
instance: ReturnType<UseOverlayScrollbarsInstance>;
|
|
||||||
options?: Options;
|
|
||||||
events?: Events;
|
|
||||||
}>({
|
|
||||||
instance: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => unref(paramsRef.value.options),
|
() => unref(paramsRef.value?.options),
|
||||||
(options) => {
|
(currOptions) => {
|
||||||
const {
|
options = currOptions;
|
||||||
value: { instance },
|
|
||||||
} = variables;
|
|
||||||
|
|
||||||
variables.value.options = options;
|
|
||||||
|
|
||||||
if (OverlayScrollbars.valid(instance)) {
|
if (OverlayScrollbars.valid(instance)) {
|
||||||
instance.options(options || {}, true);
|
instance.options(options || {}, true);
|
||||||
@@ -61,13 +54,9 @@ export const useOverlayScrollbars = (
|
|||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => unref(paramsRef.value.events),
|
() => unref(paramsRef.value?.events),
|
||||||
(events) => {
|
(currEvents) => {
|
||||||
const {
|
events = currEvents;
|
||||||
value: { instance },
|
|
||||||
} = variables;
|
|
||||||
|
|
||||||
variables.value.events = events;
|
|
||||||
|
|
||||||
if (OverlayScrollbars.valid(instance)) {
|
if (OverlayScrollbars.valid(instance)) {
|
||||||
instance.on(
|
instance.on(
|
||||||
@@ -83,23 +72,12 @@ export const useOverlayScrollbars = (
|
|||||||
return [
|
return [
|
||||||
(target: InitializationTarget): OverlayScrollbars => {
|
(target: InitializationTarget): OverlayScrollbars => {
|
||||||
// if already initialized return the current instance
|
// if already initialized return the current instance
|
||||||
const {
|
|
||||||
value: { instance, options, events },
|
|
||||||
} = variables;
|
|
||||||
if (OverlayScrollbars.valid(instance)) {
|
if (OverlayScrollbars.valid(instance)) {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currOptions = options || {};
|
return (instance = OverlayScrollbars(target, options || {}, events || {}));
|
||||||
const currEvents = events || {};
|
|
||||||
const osInstance = (variables.value.instance = OverlayScrollbars(
|
|
||||||
target,
|
|
||||||
currOptions,
|
|
||||||
currEvents
|
|
||||||
));
|
|
||||||
|
|
||||||
return osInstance;
|
|
||||||
},
|
},
|
||||||
() => variables.value.instance,
|
() => instance,
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ describe('useOverlayScrollbars', () => {
|
|||||||
render({
|
render({
|
||||||
setup() {
|
setup() {
|
||||||
const div = ref<HTMLElement | null>(null);
|
const div = ref<HTMLElement | null>(null);
|
||||||
const params = ref<{ options?: PartialOptions; events?: EventListeners }>({});
|
const params = ref<{ options?: PartialOptions; events?: EventListeners } | undefined>();
|
||||||
const [initialize, instance] = useOverlayScrollbars(params);
|
const [initialize, instance] = useOverlayScrollbars(params);
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
@@ -108,7 +108,7 @@ describe('useOverlayScrollbars', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
watchPostEffect(() => {
|
watchPostEffect(() => {
|
||||||
if (params.value.events?.updated) {
|
if (params.value?.events?.updated) {
|
||||||
instance()?.update(true);
|
instance()?.update(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -118,6 +118,7 @@ describe('useOverlayScrollbars', () => {
|
|||||||
<div ref={div} />
|
<div ref={div} />
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
params.value = {};
|
||||||
params.value.options = {};
|
params.value.options = {};
|
||||||
params.value.events = {};
|
params.value.events = {};
|
||||||
params.value.options.paddingAbsolute = true;
|
params.value.options.paddingAbsolute = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user