|
1 | 1 | import { DynamicSheet, DynamiCSS } from "@dynamicss/dynamicss"; |
2 | 2 | import * as React from "react"; |
3 | 3 |
|
4 | | -export const useAvatarStyle = (borderRadius: string | undefined): boolean => { |
| 4 | +export const useAvatarStyle = (avatarId: string, borderRadius: string | undefined): string | undefined => { |
5 | 5 | const [idAvatarStyles, setIdAvatarStyles] = React.useState<string>(""); |
6 | 6 | const [styleInjected, setStyleInjected] = React.useState<boolean>(false); |
7 | | - |
| 7 | + const [classNameBorder, setClassNameBorder] = React.useState<string | undefined>(undefined); |
| 8 | +console.log("borderRadius",borderRadius); |
8 | 9 | /** |
9 | 10 | * creates a dynamic css sheet for avatar |
10 | 11 | * @param borderRadius the border radius |
11 | 12 | * @returns a dynamic css sheet |
12 | 13 | */ |
13 | | - const makeDynamicAvatarCSSRules = (borderRadius: string | undefined): DynamicSheet => { |
| 14 | + const makeDynamicAvatarCSSRules = (avatarId: string, borderRadius: string | undefined): DynamicSheet => { |
| 15 | + const finalIdStyle: string = !borderRadius ? "-default" : `-${avatarId}`; |
14 | 16 | const styleSheet: DynamicSheet = DynamiCSS.makeStyleSheet({ |
15 | | - id: "avatar-styles", |
| 17 | + id: "fui-avatar-styles" + finalIdStyle, |
16 | 18 | sheetRules: [ |
17 | 19 | { |
18 | | - className: "fui-avatar-border", |
| 20 | + className: "fui-avatar-border" + finalIdStyle, |
19 | 21 | rules: { |
20 | 22 | borderRadius: `${borderRadius || "6px"} !important`, |
21 | 23 | } |
@@ -44,44 +46,45 @@ export const useAvatarStyle = (borderRadius: string | undefined): boolean => { |
44 | 46 | DynamiCSS.removeStyleSheet(idAvatarStyles); |
45 | 47 | return; |
46 | 48 | } */ |
47 | | - let idStyle: string = "avatar-styles"; |
48 | | - const styleSheet: DynamicSheet = makeDynamicAvatarCSSRules(borderRadius); |
49 | | - // check if classname was added |
50 | | - // if yes, edit css |
51 | | - // if not insert css |
52 | | - if (!styleInjected) { |
53 | | - console.log("avatar, no css, inserting"); |
54 | | - idStyle = DynamiCSS.insertStyleSheet(styleSheet); |
55 | | - console.log("avatar, no css, inserted OK", idStyle); |
| 49 | + let idStyle: string = ""; |
| 50 | + const styleSheet: DynamicSheet = makeDynamicAvatarCSSRules(avatarId, borderRadius); |
| 51 | + //check if default is in DOM |
| 52 | + if (!borderRadius && !styleInjected) { |
| 53 | + if (DynamiCSS.existStyleSheet("fui-avatar-styles-default")) { |
| 54 | + setStyleInjected(true); |
| 55 | + setIdAvatarStyles("fui-avatar-styles-default"); |
| 56 | + } else { |
| 57 | + idStyle = DynamiCSS.insertStyleSheet(styleSheet); |
| 58 | + setIdAvatarStyles(idStyle); |
| 59 | + if (idStyle !== "") { |
| 60 | + setStyleInjected(true); |
| 61 | + } |
| 62 | + } |
| 63 | + } else if (!styleInjected) { |
| 64 | + // check if classname was added |
| 65 | + // if yes, edit css |
| 66 | + // if not insert css |
| 67 | + console.log("avatar, no css, inserting"); |
| 68 | + idStyle = DynamiCSS.insertStyleSheet(styleSheet); |
| 69 | + console.log("avatar, no css, inserted OK", idStyle); |
56 | 70 |
|
57 | | - setIdAvatarStyles(idStyle); |
| 71 | + setIdAvatarStyles(idStyle); |
58 | 72 |
|
59 | | - if (idStyle !== "") { |
60 | | - setStyleInjected(true); |
| 73 | + if (idStyle !== "") { |
| 74 | + setStyleInjected(true); |
| 75 | + } |
| 76 | + } else { |
| 77 | + console.log("avatar, catch css, modifiying", idAvatarStyles); |
| 78 | + DynamiCSS.editStyleSheet(idAvatarStyles, styleSheet.sheetRules || []); |
61 | 79 | } |
62 | | - } else { |
63 | | - console.log("avatar, catch css, modifiying", idAvatarStyles); |
64 | | - DynamiCSS.editStyleSheet(idAvatarStyles, styleSheet.sheetRules || []); |
65 | | - } |
| 80 | + setClassNameBorder("fui-avatar-border-"+avatarId); |
66 | 81 | // eslint-disable-next-line |
67 | 82 | }, [borderRadius]); |
68 | 83 |
|
69 | 84 |
|
70 | | - /* React.useEffect(() => { |
71 | | - |
72 | | - return () => { |
73 | | - console.log("avatar, deleting init", styleInjected, idAvatarStyles); |
74 | | - if (styleInjected) { |
75 | | - console.log("avatar, catch css delete"); |
76 | | - |
77 | | - DynamiCSS.removeStyleSheet(idAvatarStyles); |
78 | | - } |
79 | | - setIdAvatarStyles(""); |
80 | | - setStyleInjected(false); |
81 | | - } |
82 | | - }, [idAvatarStyles, styleInjected]); */ |
83 | 85 |
|
84 | | - return styleInjected; |
| 86 | + |
| 87 | + return classNameBorder; |
85 | 88 | } |
86 | 89 |
|
87 | 90 |
|
0 commit comments