GoonCord/src/lib/ui/styles.ts

34 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-05-22 19:41:00 +08:00
import { lazyDestructure } from "@lib/utils/lazy";
2024-05-23 16:21:22 +08:00
import { findByProps, findByPropsProxy } from "@metro/utils";
import { isSemanticColor, resolveSemanticColor } from "@ui/color";
import { DiscordTextStyles } from "@ui/types";
import { ImageStyle, StyleSheet, TextStyle, ViewStyle } from "react-native";
type NamedStyles<T> = { [P in keyof T]: ViewStyle | TextStyle | ImageStyle };
2024-05-23 16:21:22 +08:00
const CompatfulRedesign = findByPropsProxy("createStyles");
2024-05-22 19:41:00 +08:00
export const { TextStyleSheet } = lazyDestructure(() => findByProps("TextStyleSheet")) as unknown as {
TextStyleSheet: { [key in DiscordTextStyles]: TextStyle; };
};
export function createStyles<T extends NamedStyles<T>>(sheet: T | ((props: any) => T)): () => T {
return CompatfulRedesign.createStyles(sheet);
}
// Reimplementation of Discord's createThemedStyleSheet, which was removed since 204201
// Not exactly a 1:1 reimplementation, but sufficient to keep compatibility with existing plugins
export function createThemedStyleSheet<T extends StyleSheet.NamedStyles<T>>(sheet: T) {
for (const key in sheet) {
// @ts-ignore
sheet[key] = new Proxy(StyleSheet.flatten(sheet[key]), {
get(target, prop, receiver) {
const res = Reflect.get(target, prop, receiver);
2024-03-07 09:38:44 +08:00
return isSemanticColor(res) ? resolveSemanticColor(res) : res;
}
});
}
return sheet;
}