Skip to content

Commit

Permalink
feat(settings): new prefers-reduced-motion setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhaocl1997 committed Aug 22, 2022
1 parent c0ee1e9 commit 9962228
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 49 deletions.
31 changes: 0 additions & 31 deletions src/assets/styles/base/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,3 @@ body {
height: 100%;
overflow: hidden;
}

html[color-mode="cafe"] {
filter: sepia(0.9) hue-rotate(315deg) brightness(0.9);
}

html[color-mode="contrast"] {
filter: contrast(2);
}

html[color-mode="grayscale"] {
filter: grayscale(100%);
}

html[color-mode="inverted"] {
filter: invert(100%);
}

html[color-mode="grayscale-inverted"] {
filter: grayscale(100%) invert(100%);
}

html[reduce="true"] {
transition-duration: 0.001ms !important;
animation: none !important;
animation-duration: 0.001ms !important;
animation-iteration-count: 1 !important;

--animate-duration: 0 !important;
--animate-deply: 0 !important;
--animate-repeat: 0 !important;
}
19 changes: 19 additions & 0 deletions src/assets/styles/base/_colorMode.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
html[color-mode="cafe"] {
filter: sepia(0.9) hue-rotate(315deg) brightness(0.9);
}

html[color-mode="contrast"] {
filter: contrast(2);
}

html[color-mode="grayscale"] {
filter: grayscale(100%);
}

html[color-mode="inverted"] {
filter: invert(100%);
}

html[color-mode="grayscale-inverted"] {
filter: grayscale(100%) invert(100%);
}
33 changes: 33 additions & 0 deletions src/assets/styles/base/_reducedMotion.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// this is for manual reduce motion
html[reduce="true"] *,
::before,
::after {
transition-duration: 0.001ms !important;
transition-delay: 0s !important;
animation: none !important;
animation-duration: 0.001ms !important;
animation-delay: -0.001ms !important;
animation-iteration-count: 1 !important;

--animate-duration: 0 !important;
--animate-delay: 0 !important;
--animate-repeat: 0 !important;
}

// this is for system reduce motion prefers
@media (prefers-reduced-motion: reduce) {
*,
::before,
::after {
transition-duration: 0.001ms !important;
transition-delay: 0s !important;
animation: none !important;
animation-duration: 0.001ms !important;
animation-delay: -0.001ms !important;
animation-iteration-count: 1 !important;

--animate-duration: 0 !important;
--animate-delay: 0 !important;
--animate-repeat: 0 !important;
}
}
17 changes: 9 additions & 8 deletions src/assets/styles/main.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@charset "UTF-8";

@import './abstracts/utils';
@import './abstracts/transition';
@import './base/base';
@import './base/scrollBar';
@import './components/select';
@import './components/loading';
@import './layout/tabs';
@import "./abstracts/utils";
@import "./abstracts/transition";
@import "./base/base";
@import "./base/scrollBar";
@import "./base/colorMode";
@import "./base/reducedMotion";
@import "./components/select";
@import "./components/loading";
@import "./layout/tabs";
27 changes: 25 additions & 2 deletions src/components/App/AppSettings/form/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
<script lang="ts" setup>
import { appRelatives, modalColor } from '../shared'
const preferredReducedMotion = usePreferredReducedMotion()
const appSettings = useAppStoreSetting()
const getSystemCanAnimtion = computed(
() => preferredReducedMotion.value === 'no-preference'
)
const getCanAnimation = computed(
() => getSystemCanAnimtion && appSettings.settings.app.reducedMotion
)
const [register] = useForm<typeof appRelatives.value>({
localeUniqueKey: 'app.app',
showFeedback: false,
Expand Down Expand Up @@ -88,14 +100,19 @@
formProp: {
path: 'showAnimation',
},
componentProp: {
disabled: getCanAnimation,
},
},
{
type: 'Base:Select',
formProp: {
path: 'animationMode',
},
componentProp: {
disabled: computed(() => !appRelatives.value.showAnimation),
disabled: computed(
() => getCanAnimation.value || !appRelatives.value.showAnimation
),
options: Object.values(AppConstAnimationMode).map((i) => ({
label: i,
value: i,
Expand All @@ -110,8 +127,10 @@
componentProp: {
disabled: computed(
() =>
getCanAnimation.value ||
appRelatives.value.animationMode ===
AppConstAnimationMode.SCOPE || !appRelatives.value.showAnimation
AppConstAnimationMode.SCOPE ||
!appRelatives.value.showAnimation
),
tooltip: true,
},
Expand Down Expand Up @@ -204,6 +223,10 @@
formProp: {
path: 'reducedMotion',
},
componentProp: {
// disable this switch when system prefers reduced motion
disabled: !getSystemCanAnimtion,
},
},
],
})
Expand Down
18 changes: 12 additions & 6 deletions src/components/Extra/Scrollbar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
setup(props, { attrs, slots, emit, expose }) {
const id = ref(genString(8))
const appSettings = useAppStoreSetting()
const getBehavior = computed(() =>
appSettings.settings.app.reducedMotion ? 'auto' : 'smooth'
)
const scrollRef =
ref<Nullable<ScrollbarInst & { scrollbarInstRef: Recordable }>>(null)
Expand All @@ -47,14 +53,14 @@
scrollTo: (opt) => {
scrollRef.value!.scrollTo({
...(opt as Object),
behavior: props.behavior,
behavior: getBehavior.value,
})
},
scrollToStart: () => {
scrollRef.value!.scrollTo({
position: 'top',
behavior: props.behavior,
behavior: getBehavior.value,
})
},
Expand All @@ -64,12 +70,12 @@
? {
left: scrollRef.value!.scrollbarInstRef.containerRef
?.scrollWidth,
behavior: props.behavior,
behavior: getBehavior.value,
}
: {
top: scrollRef.value!.scrollbarInstRef.containerRef
?.scrollHeight,
behavior: props.behavior,
behavior: getBehavior.value,
}
)
},
Expand All @@ -87,11 +93,11 @@
props.vertical
? {
left: node['offsetLeft'],
behavior: props.behavior,
behavior: getBehavior.value,
}
: {
top: node['offsetTop'],
behavior: props.behavior,
behavior: getBehavior.value,
}
)
},
Expand Down
10 changes: 8 additions & 2 deletions src/components/Vendor/echarts/src/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
const appDark = useAppStoreDark()
const appLocale = useAppStoreLocale()
const appSettings = useAppStoreSetting()
const getSkinName = computed(() => (appDark.isDark ? 'dark' : undefined))
Expand Down Expand Up @@ -55,8 +56,13 @@
chartInst.value!.setOption(
appDark.isDark
? Object.assign(props.option, { backgroundColor: 'transparent' })
: props.option
? Object.assign(props.option, {
backgroundColor: 'transparent',
animation: !appSettings.settings.app.reducedMotion,
})
: Object.assign(props.option, {
animation: !appSettings.settings.app.reducedMotion,
})
)
}
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/core/useAppReducedMotion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const useAppReducedMotion = () => {
const appSetting = useAppStoreSetting()
const prefersReducedMotion = usePreferredReducedMotion()

watch(
() => appSetting.settings.app.reducedMotion,
Expand All @@ -10,4 +11,9 @@ export const useAppReducedMotion = () => {
immediate: true,
}
)

watchEffect(() => {
appSetting.settings.app.reducedMotion =
prefersReducedMotion.value === 'reduce'
})
}
3 changes: 3 additions & 0 deletions src/views/index/home/components/NumberCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
:from="0"
:to="number"
show-separator
:duration="appSettings.settings.app.reducedMotion ? 0 : 3000"
></n-number-animation>
<w-transition appear name="slide-up">
Expand Down Expand Up @@ -95,6 +96,8 @@
const props = defineProps<InternalProps>()
const appSettings = useAppStoreSetting()
const trend = ref(0)
const showTrend = ref(true)
const isPositive = ref<boolean | undefined>()
Expand Down

0 comments on commit 9962228

Please sign in to comment.