Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add responsive component #704

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
"echarts": "^5.2.2",
"echarts-gl": "^2.0.8",
"js-sha256": "^0.9.0",
"lodash.throttle": "^4.1.1",
"overlayscrollbars": "^1.13.1",
"overlayscrollbars-vue": "^0.2.2",
"regenerator-runtime": "^0.13.9",
"resize-observer-polyfill": "^1.5.1",
"semver": "^7.3.5",
"uuid": "^8.3.2",
"vue": "^2.6.14",
Expand All @@ -61,6 +63,7 @@
"devDependencies": {
"@intlify/vite-plugin-vue-i18n": "^2.5.0",
"@mdi/js": "^6.5.95",
"@types/lodash.throttle": "^4.1.6",
"@types/semver": "^7.3.8",
"@types/uuid": "^8.3.1",
"@typescript-eslint/eslint-plugin": "^5.11.0",
Expand Down
48 changes: 48 additions & 0 deletions src/components/mixins/responsive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Component from 'vue-class-component'
import BaseMixin from './base'
import { Prop } from 'vue-property-decorator'
import throttle from 'lodash.throttle'

export type ResponsiveElement = {
is: {
[key: string]: boolean
}
}

@Component
export default class ResponsiveMixin extends BaseMixin {
@Prop() protected declare breakpoints: {
[key: string]: (el: DOMRect) => boolean
}

observer?: ResizeObserver

el: ResponsiveElement = {
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](cr))
}
}
}
2 changes: 2 additions & 0 deletions src/components/panels/GcodefilesPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ import {
mdiPencil,
mdiDelete,
mdiCloseThick,
mdiClose,
} from '@mdi/js'

interface draggingFile {
Expand Down Expand Up @@ -609,6 +610,7 @@ export default class GcodefilesPanel extends Mixins(BaseMixin) {
mdiPlay = mdiPlay
mdiPlaylistPlus = mdiPlaylistPlus
mdiFire = mdiFire
mdiClose = mdiClose
mdiVideo3d = mdiVideo3d
mdiCloudDownload = mdiCloudDownload
mdiRenameBox = mdiRenameBox
Expand Down
26 changes: 26 additions & 0 deletions src/components/ui/Responsive.vue
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>
File renamed without changes.
16 changes: 16 additions & 0 deletions src/directives/responsive-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import throttle from 'lodash.throttle'
import Vue from 'vue'

Vue.directive('responsive-class', {
inserted(el, conds) {
const handleResize = throttle((entries: ResizeObserverEntry[]) => {
const cr = entries[0].contentRect
for (const breakpoint in conds.value) {
el.classList.toggle(breakpoint, conds.value[breakpoint](cr))
}
}, 50)

const observer = new ResizeObserver(handleResize)
observer.observe(el)
},
})
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'regenerator-runtime' // async polyfill used by the gcodeviewer
import 'resize-observer-polyfill' // polyfill needed by the responsive class detection

import Vue from 'vue'
import App from '@/App.vue'
import vuetify from '@/plugins/vuetify'
import i18n from '@/plugins/i18n'
import './plugins/longpress'
import store from '@/store'
import router from '@/plugins/router'

Expand Down Expand Up @@ -50,6 +50,11 @@ Vue.use(OverlayScrollbarsPlugin, {
},
})

// Directives

pataar marked this conversation as resolved.
Show resolved Hide resolved
import './directives/longpress'
import './directives/responsive-class'

// Echarts
import ECharts from 'vue-echarts'
import { use } from 'echarts/core'
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"sourceMap": true,
"skipLibCheck": true,
"baseUrl": ".",
"types": [
"cypress",
Expand Down