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(QImg): add spinner-delay prop #16932

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 22 additions & 3 deletions ui/src/components/img/QImg.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export default createComponent({
noTransition: Boolean,

spinnerColor: String,
spinnerSize: String
spinnerSize: String,
spinnerDelay: {
type: Number,
default: 0
},
},

emits: [ 'load', 'error' ],
Expand All @@ -69,7 +73,7 @@ export default createComponent({
const naturalRatio = ref(props.initialRatio)
const ratioStyle = useRatio(props, naturalRatio)

let loadTimer = null, isDestroyed = false
let loadTimer = null, showSpinnerTimer = null, isDestroyed = false

const images = [
ref(null),
Expand All @@ -80,6 +84,7 @@ export default createComponent({

const isLoading = ref(false)
const hasError = ref(false)
const canShowSpinner = ref(false)

const classes = computed(() =>
`q-img q-img--${ props.noNativeMenu === true ? 'no-' : '' }menu`
Expand Down Expand Up @@ -133,6 +138,20 @@ export default createComponent({
}
else {
isLoading.value = true

if (showSpinnerTimer !== null) {
clearTimeout(showSpinnerTimer)
showSpinnerTimer = null
}

canShowSpinner.value = props.spinnerDelay === 0

if (props.spinnerDelay) {
showSpinnerTimer = setTimeout(() => {
showSpinnerTimer = null
canShowSpinner.value = true
}, props.spinnerDelay)
}
}

images[ position.value ].value = imgProps
Expand Down Expand Up @@ -241,7 +260,7 @@ export default createComponent({
slots.loading !== void 0
? slots.loading()
: (
props.noSpinner === true
props.noSpinner === true || canShowSpinner.value === false
? void 0
: [
h(QSpinner, {
Expand Down
8 changes: 8 additions & 0 deletions ui/src/components/img/QImg.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@
"category": "style"
},

"spinner-delay": {
"type": "Number",
"desc": "Delay showing the spinner when image changes; gives time for the browser to load the image from cache to prevent flashing the spinner unnecesarily",
"default": 0,
"examples": [ 500, ":spinner-delay=\"550\"" ],
"category": "behavior"
},

"no-spinner": {
"type": "Boolean",
"desc": "Do not display the default spinner while waiting for the image to be loaded; It is overriden by the 'loading' slot when one is present",
Expand Down
Loading