Create a homemade skeleton

with moti and css animations
This commit is contained in:
Zoe Roux 2022-12-09 02:04:31 +09:00
parent 26e40adb21
commit f834e08273
6 changed files with 101 additions and 32 deletions

View File

@ -24,7 +24,7 @@ import { ReactNode, useState } from "react";
import NextApp, { AppContext, type AppProps } from "next/app";
import { createTheme, ThemeProvider as MTheme } from "@mui/material";
import { Hydrate, QueryClientProvider } from "@tanstack/react-query";
import { ThemeSelector as KThemeSelector, WebTooltip } from "@kyoo/primitives";
import { SkeletonCss, ThemeSelector as KThemeSelector, WebTooltip } from "@kyoo/primitives";
import { createQueryClient, fetchQuery, QueryIdentifier, QueryPage } from "@kyoo/models";
import { useTheme, useMobileHover } from "yoshiki/web";
import superjson from "superjson";
@ -68,6 +68,7 @@ const GlobalCssTheme = () => {
}
`}</style>
<WebTooltip theme={theme} />
<SkeletonCss />
</>
);
};

View File

@ -18,24 +18,95 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/
import { View } from "moti";
import { Skeleton as MSkeleton } from "moti/skeleton";
import { ComponentProps } from "react";
import { useYoshiki, rem, px, Stylable } from "yoshiki/native";
import { LinearGradient as LG } from "expo-linear-gradient";
import { motify } from "moti";
import { useState } from "react";
import { Platform, View, ViewProps } from "react-native";
import { px, rem, useYoshiki, percent } from "yoshiki/native";
const LinearGradient = motify(LG)();
export const SkeletonCss = () => (
<style jsx global>{`
@keyframes skeleton {
0% {
transform: translateX(-100%);
}
50% {
transform: translateX(100%);
}
100% {
transform: translateX(100%);
}
}
`}</style>
);
export const Skeleton = ({
style,
children,
variant = "text",
...props
}: Omit<ComponentProps<typeof MSkeleton>, "children"> & {
children: ComponentProps<typeof MSkeleton>["children"] | boolean;
} & Stylable) => {
const { css } = useYoshiki();
}: Omit<ViewProps, "children"> & {
children?: JSX.Element | boolean | null;
variant?: "text" | "round" | "custom";
}) => {
const { css, theme } = useYoshiki();
const [width, setWidth] = useState<number | undefined>(undefined);
const perc = (v: number) => (v / 100) * width!;
if (children && children !== true) return children;
return (
<View {...css({ margin: px(2) }, { style })}>
<MSkeleton colorMode="light" radius={6} height={rem(1.2)} {...props}>
{children !== true ? children || undefined : undefined}
</MSkeleton>
<View
onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
{...css(
[
{
bg: (theme) => theme.overlay0,
margin: px(2),
position: "relative",
overflow: "hidden",
borderRadius: px(6),
},
variant === "text" && {
width: percent(75),
height: rem(1.2),
},
variant === "round" && {
borderRadius: 9999999,
},
],
props,
)}
>
<LinearGradient
start={{ x: 0, y: 0.5 }}
end={{ x: 1, y: 0.5 }}
colors={["transparent", theme.overlay1, "transparent"]}
transition={{
loop: true,
repeatReverse: false,
}}
animate={{
translateX: width
? [perc(-100), { value: perc(100), type: "timing", duration: 800, delay: 800 }]
: undefined,
}}
{...css([
{
position: "absolute",
top: 0,
bottom: 0,
left: 0,
right: 0,
},
Platform.OS === "web" && {
// @ts-ignore Web only properties
animation: "skeleton 1.6s linear 0.5s infinite",
transform: "translateX(-100%)",
},
])}
/>
</View>
);
};

View File

@ -29,8 +29,8 @@ export const catppuccin: ThemeBuilder = {
light: {
// Catppuccin latte
appbar: "#e64553",
contrast: "#cdd6f4",
subcontrast: "#bac2de",
overlay0: "#9ca0b0",
overlay1: "#7c7f93",
default: {
background: "#eff1f5",
accent: "#ea76cb",
@ -59,8 +59,8 @@ export const catppuccin: ThemeBuilder = {
dark: {
// Catppuccin mocha
appbar: "#94e2d5",
contrast: "#cdd6f4",
subcontrast: "#bac2de",
overlay0: "#6c7086",
overlay1: "#9399b2",
default: {
background: "#1e1e2e",
accent: "##f5c2e7",

View File

@ -33,20 +33,17 @@ type ThemeSettings = {
type Mode = {
appbar: Property.Color;
/*
* The color used in texts or button that are hover black shades on images (ShowHeader, player...)
*/
contrast: Property.Color;
subcontrast: Property.Color;
overlay0: Property.Color;
overlay1: Property.Color;
variant: Variant;
colors: {
red: Property.Color,
green: Property.Color,
blue: Property.Color,
yellow: Property.Color,
white: Property.Color,
black: Property.Color,
}
red: Property.Color;
green: Property.Color;
blue: Property.Color;
yellow: Property.Color;
white: Property.Color;
black: Property.Color;
};
};
type Variant = {

View File

@ -33,4 +33,4 @@ export const DefaultLayout = (page: ReactElement) => {
);
};
DefaultLayout.query = () => [Navbar.query()];
DefaultLayout.getFetchUrls = () => [Navbar.query()];

View File

@ -95,7 +95,7 @@ export const Navbar = (props: Stylable) => {
{library.name}
</A>
) : (
<Skeleton key={i} width={rem(5)} colorMode="light" />
<Skeleton key={i} {...css({ width: rem(5) })} />
)
}
</Fetch>