-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Component from 'vue-class-component' | ||
import BaseMixin from './base' | ||
import { Prop } from 'vue-property-decorator' | ||
import throttle from 'lodash.throttle' | ||
|
||
export type ResponsiveElement = { | ||
width: number | ||
height: number | ||
is: { | ||
[key: string]: boolean | ||
} | ||
} | ||
|
||
@Component | ||
export default class ResponsiveMixin extends BaseMixin { | ||
@Prop() protected declare breakpoints: { | ||
[key: string]: (el: ResponsiveElement) => boolean | ||
} | ||
|
||
observer?: ResizeObserver | ||
|
||
el: ResponsiveElement = { | ||
width: 0, | ||
height: 0, | ||
is: {}, | ||
} | ||
|
||
mounted() { | ||
if (this.breakpoints) { | ||
this.$nextTick(() => { | ||
this.observer = new ResizeObserver(throttle(this.onResize, 50)) | ||
if (this.$el instanceof Element) { | ||
this.observer.observe(this.$el) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
beforeDestroy() { | ||
if (this.$el instanceof Element) { | ||
this.observer?.unobserve(this.$el) | ||
} | ||
} | ||
|
||
private onResize(entries: ResizeObserverEntry[]) { | ||
const cr = entries[0].contentRect | ||
const conds = this.breakpoints | ||
for (const breakpoint in conds) { | ||
this.$set( | ||
this.el.is, | ||
breakpoint, | ||
conds[breakpoint]({ | ||
...this.el, | ||
width: cr.width, | ||
height: cr.height, | ||
}) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<template> | ||
<div> | ||
<div v-if="!noHide && !init" style="visibility: hidden"> | ||
<slot :el="el"></slot> | ||
</div> | ||
|
||
<slot :el="el"></slot> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import Component from 'vue-class-component' | ||
import { Mixins, Prop } from 'vue-property-decorator' | ||
import ResponsiveMixin from '@/components/mixins/responsive' | ||
@Component | ||
export default class Responsive extends Mixins(ResponsiveMixin) { | ||
@Prop({ default: false }) protected declare noHide: boolean | ||
init = false | ||
mounted() { | ||
this.init = true | ||
} | ||
} | ||
</script> |