Skip to content

Commit de2ba93

Browse files
committed
refactor: rename directives & affix use setup
1 parent 64673f2 commit de2ba93

File tree

5 files changed

+81
-96
lines changed

5 files changed

+81
-96
lines changed

src/components/affix/Affix.vue

Lines changed: 52 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,62 @@
11
<template>
2-
<div class="hidden-print">
2+
<div ref="el" class="hidden-print">
33
<div v-scroll="onScroll" :class="classes" :style="styles">
44
<slot></slot>
55
</div>
66
</div>
77
</template>
88

9-
<script>
10-
import scroll from './../../directives/scroll';
9+
<script setup>
10+
import vScroll from './../../directives/scroll';
11+
import { computed, defineProps, ref, nextTick } from 'vue';
1112
12-
export default {
13-
directives: {
14-
scroll,
13+
const props = defineProps({
14+
offset: {
15+
type: Number,
16+
default: 0,
1517
},
16-
props: {
17-
offset: {
18-
type: Number,
19-
default: 0,
20-
},
21-
},
22-
data() {
23-
return {
24-
affixed: false,
25-
};
26-
},
27-
computed: {
28-
classes() {
29-
return {
30-
affix: this.affixed,
31-
};
32-
},
33-
styles() {
34-
return {
35-
top: this.affixed ? this.offset + 'px' : null,
36-
};
37-
},
38-
},
39-
methods: {
40-
// from https://github.com/ant-design/ant-design/blob/master/components/affix/index.jsx#L20
41-
onScroll() {
42-
// if is hidden don't calculate anything
43-
if (
44-
!(
45-
this.$el.offsetWidth ||
46-
this.$el.offsetHeight ||
47-
this.$el.getClientRects().length
48-
)
49-
) {
50-
return;
51-
}
52-
// get window scroll and element position to detect if have to be normal or affixed
53-
const scroll = {};
54-
const element = {};
55-
const rect = this.$el.getBoundingClientRect();
56-
const body = document.body;
57-
const types = ['Top', 'Left'];
58-
types.forEach((type) => {
59-
const t = type.toLowerCase();
60-
scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset'];
61-
element[t] =
62-
scroll[t] +
63-
rect[t] -
64-
(this.$el['client' + type] || body['client' + type] || 0);
65-
});
66-
const fix = scroll.top > element.top - this.offset;
67-
if (this.affixed !== fix) {
68-
this.affixed = fix;
69-
this.$emit(this.affixed ? 'affix' : 'unfix');
70-
this.$nextTick(() => {
71-
this.$emit(this.affixed ? 'affixed' : 'unfixed');
72-
});
73-
}
74-
},
75-
},
76-
};
18+
});
19+
20+
const emit = defineEmits(['affix', 'affixed', 'unfix', 'unfixed']);
21+
22+
const el = ref(null);
23+
const affixed = ref(false);
24+
const classes = computed(() => ({ affix: affixed.value }));
25+
const styles = computed(() => ({
26+
top: affixed.value ? props.offset + 'px' : null,
27+
}));
28+
29+
function onScroll() {
30+
if (
31+
!(
32+
el.value?.offsetWidth ||
33+
el.value?.offsetHeight ||
34+
el.value?.getClientRects().length
35+
)
36+
) {
37+
return;
38+
}
39+
// get window scroll and element position to detect if have to be normal or affixed
40+
const scroll = {};
41+
const element = {};
42+
const rect = el.value.getBoundingClientRect();
43+
const body = document.body;
44+
const types = ['Top', 'Left'];
45+
types.forEach((type) => {
46+
const t = type.toLowerCase();
47+
scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset'];
48+
element[t] =
49+
scroll[t] +
50+
rect[t] -
51+
(el.value['client' + type] || body['client' + type] || 0);
52+
});
53+
const fix = scroll.top > element.top - props.offset;
54+
if (affixed.value !== fix) {
55+
affixed.value = fix;
56+
emit(affixed.value ? 'affix' : 'unfix');
57+
nextTick(() => {
58+
emit(affixed.value ? 'affixed' : 'unfixed');
59+
});
60+
}
61+
}
7762
</script>

src/directives/popover/popover.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { hasOwnProperty } from '../../utils/object.utils';
44

55
const INSTANCE = '_uiv_popover_instance';
66

7-
const bind = (el, binding) => {
7+
const mounted = (el, binding) => {
88
// console.log('bind')
9-
unbind(el);
9+
unmounted(el);
1010
const options = [];
1111
for (const key in binding.modifiers) {
1212
if (hasOwnProperty(binding.modifiers, key) && binding.modifiers[key]) {
@@ -54,7 +54,7 @@ const bind = (el, binding) => {
5454
el[INSTANCE] = container;
5555
};
5656

57-
const unbind = (el) => {
57+
const unmounted = (el) => {
5858
// console.log('unbind')
5959
const instance = el[INSTANCE];
6060
if (instance) {
@@ -63,11 +63,11 @@ const unbind = (el) => {
6363
delete el[INSTANCE];
6464
};
6565

66-
const update = (el, binding) => {
66+
const updated = (el, binding) => {
6767
// console.log('update')
6868
if (binding.value !== binding.oldValue) {
69-
bind(el, binding);
69+
mounted(el, binding);
7070
}
7171
};
7272

73-
export default { mounted: bind, unmounted: unbind, updated: update };
73+
export default { mounted, unmounted, updated };

src/directives/scroll.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ import { isFunction } from '../utils/object.utils';
44
const HANDLER = '_uiv_scroll_handler';
55
const events = [EVENTS.RESIZE, EVENTS.SCROLL];
66

7-
const bind = (el, binding) => {
7+
const mounted = (el, binding) => {
88
const callback = binding.value;
99
if (!isFunction(callback)) {
1010
return;
1111
}
12-
unbind(el);
12+
unmounted(el);
1313
el[HANDLER] = callback;
1414
events.forEach((event) => {
1515
on(window, event, el[HANDLER]);
1616
});
1717
};
1818

19-
const unbind = (el) => {
19+
const unmounted = (el) => {
2020
events.forEach((event) => {
2121
off(window, event, el[HANDLER]);
2222
});
2323
delete el[HANDLER];
2424
};
2525

26-
const update = (el, binding) => {
26+
const updated = (el, binding) => {
2727
if (binding.value !== binding.oldValue) {
28-
bind(el, binding);
28+
mounted(el, binding);
2929
}
3030
};
3131

32-
export default { mounted: bind, unmounted: unbind, updated: update };
32+
export default { mounted, unmounted, updated };

src/directives/scrollspy/scrollspy.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ ScrollSpy.prototype.clear = function () {
141141
const INSTANCE = '_uiv_scrollspy_instance';
142142
const events = [EVENTS.RESIZE, EVENTS.SCROLL];
143143

144-
const bind = (el, binding) => {
144+
const beforeMount = (el, binding) => {
145145
// console.log('bind')
146-
unbind(el);
146+
unmounted(el);
147147
};
148148

149-
const inserted = (el, binding) => {
149+
const mounted = (el, binding) => {
150150
// console.log('inserted')
151151
const scrollSpy = new ScrollSpy(el, binding.arg, binding.value);
152152
if (scrollSpy.scrollElement) {
@@ -160,7 +160,7 @@ const inserted = (el, binding) => {
160160
el[INSTANCE] = scrollSpy;
161161
};
162162

163-
const unbind = (el) => {
163+
const unmounted = (el) => {
164164
// console.log('unbind')
165165
const instance = el[INSTANCE];
166166
if (instance && instance.scrollElement) {
@@ -171,19 +171,19 @@ const unbind = (el) => {
171171
}
172172
};
173173

174-
const update = (el, binding) => {
174+
const updated = (el, binding) => {
175175
// console.log('update')
176176
const isArgUpdated = binding.arg !== binding.oldArg;
177177
const isValueUpdated = binding.value !== binding.oldValue;
178178
if (isArgUpdated || isValueUpdated) {
179-
bind(el, binding);
180-
inserted(el, binding);
179+
beforeMount(el, binding);
180+
mounted(el, binding);
181181
}
182182
};
183183

184184
export default {
185-
beforeMount: bind,
186-
unmounted: unbind,
187-
updated: update,
188-
mounted: inserted,
185+
beforeMount,
186+
mounted,
187+
updated,
188+
unmounted,
189189
};

src/directives/tooltip/tooltip.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { removeFromDom } from '../../utils/dom.utils';
55

66
const INSTANCE = '_uiv_tooltip_instance';
77

8-
const bind = (el, binding) => {
8+
const mounted = (el, binding) => {
99
// console.log('bind')
10-
unbind(el);
10+
unmounted(el);
1111
const options = [];
1212
for (const key in binding.modifiers) {
1313
if (hasOwnProperty(binding.modifiers, key) && binding.modifiers[key]) {
@@ -55,7 +55,7 @@ const bind = (el, binding) => {
5555
el[INSTANCE] = { container, vNode };
5656
};
5757

58-
const unbind = (el) => {
58+
const unmounted = (el) => {
5959
// console.log('unbind', el[INSTANCE])
6060
const instance = el[INSTANCE];
6161
if (instance) {
@@ -67,11 +67,11 @@ const unbind = (el) => {
6767
delete el[INSTANCE];
6868
};
6969

70-
const update = (el, binding) => {
70+
const updated = (el, binding) => {
7171
// console.log('update', binding.oldValue, '->', binding.value)
7272
if (binding.value !== binding.oldValue) {
73-
bind(el, binding);
73+
mounted(el, binding);
7474
}
7575
};
7676

77-
export default { mounted: bind, unmounted: unbind, updated: update };
77+
export default { mounted, unmounted, updated };

0 commit comments

Comments
 (0)