Skip to content

Commit

Permalink
rewrite LoadingPlaceholder.vue component
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Sep 11, 2023
1 parent 99823ed commit 1404edf
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 241 deletions.
290 changes: 124 additions & 166 deletions src/components/LoadingPlaceholder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
- @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
-
- @author Joas Schilling <coding@schilljs.com>
- @author Maksim Sukharev <antreesy.web@gmail.com>
-
- @license GNU AGPL version 3 or any later version
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
Expand All @@ -18,54 +19,19 @@
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<docs>

Displays a loading placeholder for conversation messages.

The gradient animation is achieved by having two placeholder elements,
with opposite gradient directions (regular and reverse) displayed on top
of each other (overlapped with position: absolute) and then fading between
each other by animating the opacities.

</docs>

<template>
<div class="placeholder-main">
<!-- Placeholder animation -->
<template v-for="(suffix, gradientIndex) in ['-regular', '-reverse']">
<svg :key="'gradient' + suffix" :class="'placeholder-gradient placeholder-gradient' + suffix">
<defs>
<linearGradient :id="'placeholder-gradient' + suffix">
<stop offset="0%" :stop-color="(gradientIndex === 0) ? colorPlaceholderLight : colorPlaceholderDark" />
<stop offset="100%" :stop-color="(gradientIndex === 0) ? colorPlaceholderDark : colorPlaceholderLight" />
</linearGradient>
</defs>
</svg>

<ul :key="'list' + suffix" :class="'placeholder-list placeholder-list' + suffix">
<li v-for="(width, index) in placeholderData" :key="'placeholder' + suffix + index">
<svg v-if="type === 'conversations'"
class="conversation-placeholder"
xmlns="http://www.w3.org/2000/svg"
:fill="'url(#placeholder-gradient' + suffix + ')'">
<circle class="conversation-placeholder-icon" />
<rect class="conversation-placeholder-line-one" />
<rect class="conversation-placeholder-line-two" :style="width" />
</svg>
<svg v-if="type === 'messages'"
class="message-placeholder"
xmlns="http://www.w3.org/2000/svg"
:fill="'url(#placeholder-gradient' + suffix + ')'">
<circle class="message-placeholder-icon" />
<rect class="message-placeholder-line-one" />
<rect class="message-placeholder-line-two" />
<rect class="message-placeholder-line-three" />
<rect class="message-placeholder-line-four" :style="width" />
</svg>
</li>
</ul>
</template>
</div>
<ul :class="'placeholder-list placeholder-list--' + type"
:style="{ '--colorPlaceholderLight': colorPlaceholderLight, '--colorPlaceholderDark': colorPlaceholderDark }">
<li v-for="(item, index) in placeholderData" :key="index" class="placeholder-item">
<div class="placeholder-item__avatar">
<div class="placeholder-item__avatar-circle" />
</div>
<div class="placeholder-item__content" :style="{'--last-line-width': item.width}">
<div v-for="idx in item.amount" :key="idx" class="placeholder-item__content-line" />
</div>
</li>
</ul>
</template>

<script>
Expand All @@ -80,6 +46,9 @@ export default {
type: {
type: String,
required: true,
validator(value) {
return ['conversations', 'messages', 'participants'].includes(value)
},
},
count: {
type: Number,
Expand All @@ -98,8 +67,10 @@ export default {
placeholderData() {
const data = []
for (let i = 0; i < this.count; i++) {
// generate random widths
data.push('width: ' + (Math.floor(Math.random() * 20) + 30) + '%')
data.push({
amount: this.type === 'messages' ? 4 : this.type === 'conversations' ? 2 : 1,
width: (Math.floor(Math.random() * 40) + 30) + '%', // generate random widths for last line
})
}
return data
},
Expand All @@ -108,132 +79,119 @@ export default {
</script>

<style lang="scss" scoped>
@import '../assets/variables';

$clickable-area: 44px;
$margin: 8px;

.placeholder-main {
max-width: $messages-list-max-width;
position: relative;
margin: auto;
padding: 0;
}

.placeholder-list {
position: absolute;
translate: translateZ(0);
}

.placeholder-list-regular {
animation: pulse 2s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}

.placeholder-list-reverse {
animation: pulse-reverse 2s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}

.placeholder-gradient {
position: fixed;
height: 0;
width: 0;
z-index: -1;
}

.conversation-placeholder,
.message-placeholder {

&-icon {
width: $clickable-area;
height: $clickable-area;
cx: calc(#{$clickable-area} / 2);
cy: calc(#{$clickable-area} / 2);
r: calc(#{$clickable-area} / 2);
}
}

.conversation-placeholder {
width: calc(100% - 2 * #{$margin});
height: $clickable-area;
margin: $margin;

&-line-one,
&-line-two {
width: calc(100% - #{$margin + $clickable-area});
position: relative;
height: 1em;
x: $margin + $clickable-area;
}

&-line-one {
y: 5px;
}
.placeholder-list {
width: 100%;
transform: translateZ(0); // enable hardware acceleration
}

&-line-two {
y: 25px;
.placeholder-item {
display: flex;
gap: 8px;
width: 100%;

&__avatar {
&-circle {
height: var(--default-clickable-area);
width: var(--default-clickable-area);
border-radius: var(--default-clickable-area);
}
}

&__content {
display: flex;
flex-direction: column;
width: 100%;
&-line {
margin: 5px 0 4px;
width: 100%;
height: 15px;
&:last-child {
width: var(--last-line-width);
}
}
}
}

.message-placeholder {
width: $messages-list-max-width;
height: calc(#{$clickable-area} * 2);
margin: $margin auto;
padding: 0 $margin;
display: block;

&-line-one,
&-line-two,
&-line-three,
&-line-four {
width: 640px;
height: 1em;
x: $margin + $clickable-area;
}
// Conversations placeholder ruleset
.placeholder-list--conversations {
.placeholder-item {
margin: 2px 0;
padding: 8px 10px;

&-line-one {
y: 5px;
width: 175px;
}
&__content {
width: 70%;
}
}
}

&-line-two {
y: 25px;
}
// Messages placeholder ruleset
.placeholder-list--messages {
max-width: 800px;
margin: auto;

.placeholder-item {
&__avatar {
width: 52px;
padding: 18px 4px 0;

&-circle {
height: 32px;
width: 32px;
border-radius: 32px;
}
}

&__content {
padding: 12px 130px 12px 0;

&-line {
margin: 4px 0 3px;

&:first-child {
margin-bottom: 8px;
width: 20%;
}
}
}
}
}

&-line-three {
y: 45px;
}
// Participants placeholder ruleset
.placeholder-list--participants {
.placeholder-item {
gap: 12px;
margin: 4px 0;
padding: 0 4px;
height: 56px;
align-items: center;

&__avatar {
margin: auto;
}

&__content {
&-line:first-child {
width: 60%;
}
}
}
}

&-line-four {
y: 65px;
}
}
// Animation
.placeholder-item__avatar-circle,
.placeholder-item__content-line {
background-size: 200vw;
background-image: linear-gradient(90deg, var(--colorPlaceholderDark) 57%, var(--colorPlaceholderLight) 60%, var(--colorPlaceholderDark) 63%);
animation: loading-animation 4s forwards infinite linear;
will-change: background-position;
}

@keyframes pulse {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
@keyframes loading-animation {
0% {
background-position: 0;
}

@keyframes pulse-reverse {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
100% {
background-position: 140vw;
}

}
</style>
Loading

0 comments on commit 1404edf

Please sign in to comment.