Skip to content

Commit f92f28f

Browse files
committed
fix: ui improve
Signed-off-by: Innei <tukon479@gmail.com>
1 parent e8bbf2e commit f92f28f

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

packages/kami-design/components/Image/component.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { escapeHTMLTag } from '~/utils/utils'
2222

2323
import { LazyLoad } from '../Lazyload'
2424
import styles from './index.module.css'
25-
import { useCalculateSize } from './use-calculate-size'
25+
import { useCalculateNaturalSize } from './use-calculate-size'
2626

2727
interface ImageProps {
2828
defaultImage?: string
@@ -77,13 +77,20 @@ const Image: FC<
7777
!loaded && styles['image-hide'],
7878
)}
7979
data-status={loaded ? 'loaded' : 'loading'}
80+
onAnimationEnd={onImageAnimationEnd}
8081
>
8182
<img src={src} alt={alt} ref={imageRef} loading="lazy" />
8283
</div>
8384
</>
8485
)
8586
})
8687

88+
const onImageAnimationEnd: React.AnimationEventHandler<HTMLDivElement> = (
89+
e,
90+
) => {
91+
;(e.target as HTMLElement).dataset.animated = '1'
92+
}
93+
8794
export type ImageLazyRef = { status: 'loading' | 'loaded' }
8895

8996
export const ImageLazy = memo(
@@ -92,6 +99,9 @@ export const ImageLazy = memo(
9299
ImageProps &
93100
DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>
94101
>((props, ref) => {
102+
useEffect(() => {
103+
console.log('update')
104+
})
95105
const {
96106
defaultImage,
97107
src,
@@ -114,7 +124,7 @@ export const ImageLazy = memo(
114124
const placeholderRef = useRef<HTMLDivElement>(null)
115125

116126
const wrapRef = useRef<HTMLDivElement>(null)
117-
const [calculatedSize, calculateDimensions] = useCalculateSize()
127+
const [calculatedSize, calculateDimensions] = useCalculateNaturalSize()
118128

119129
const [loaded, setLoad] = useState(false)
120130
const loaderFn = useCallback(() => {

packages/kami-design/components/Image/index.module.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
animation: blur-dark 0.8s forwards ease-in-out;
3939
}
4040

41+
.lazyload-image[data-animated] {
42+
animation: none !important;
43+
}
44+
4145
.img-alt {
4246
text-align: center;
4347
font-size: 14px;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './component'
2-
export { useCalculateSize } from './use-calculate-size'
2+
export { useCalculateNaturalSize } from './use-calculate-size'
33
export { calculateDimensions } from './utils/calc-image'

packages/kami-design/components/Image/use-calculate-size.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { calculateDimensions } from '@mx-space/kami-design/components/Image/util
44

55
const initialState = { height: 0, width: 0 }
66
type Action = { type: 'set'; height: number; width: number } | { type: 'reset' }
7-
export const useCalculateSize = () => {
7+
export const useCalculateNaturalSize = () => {
88
const [state, dispatch] = useReducer(
99
(state: typeof initialState, payload: Action) => {
1010
switch (payload.type) {

src/assets/styles/extra.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pre > code {
9393
transition: color 0.5s;
9494

9595
* {
96-
background: var(--light-bg);
96+
background: transparent !important;
9797
}
9898

9999
.linenumber {

src/components/widgets/CodeHighlighter/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ export const HighLighter: FC<Props> = observer((props) => {
2323
}, [value])
2424
const isPrintMode = appUIStore.mediaType === 'print'
2525

26+
const prevThemeCSS = useRef<ReturnType<typeof loadStyleSheet>>()
27+
2628
useInsertionEffect(() => {
2729
const css = loadStyleSheet(
2830
`https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism-themes/1.9.0/prism-one-${
2931
isPrintMode ? 'light' : colorMode
3032
}.css`,
3133
)
3234

33-
return () => {
34-
css?.remove()
35+
if (prevThemeCSS.current) {
36+
const $prev = prevThemeCSS.current
37+
css.$link.onload = () => {
38+
$prev.remove()
39+
}
3540
}
41+
42+
prevThemeCSS.current = css
3643
}, [colorMode, isPrintMode])
3744
useInsertionEffect(() => {
3845
loadStyleSheet(

src/utils/load-script.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,39 @@ export function loadScript(url: string) {
5050
})
5151
}
5252

53-
const cssSet = new Set()
53+
const cssMap = new Map<string, HTMLLinkElement>()
5454

5555
export function loadStyleSheet(href: string) {
56-
if (cssSet.has(href)) {
57-
return
56+
if (cssMap.has(href)) {
57+
const $link = cssMap.get(href)!
58+
return {
59+
$link,
60+
remove: () => {
61+
$link.parentNode && $link.parentNode.removeChild($link)
62+
cssMap.delete(href)
63+
},
64+
}
5865
}
5966
const $link = document.createElement('link')
6067
$link.href = href
6168
$link.rel = 'stylesheet'
6269
$link.type = 'text/css'
6370
$link.crossOrigin = 'anonymous'
64-
cssSet.add(href)
71+
cssMap.set(href, $link)
6572

6673
$link.onerror = () => {
6774
$link.onerror = null
68-
cssSet.delete(href)
75+
cssMap.delete(href)
6976
}
77+
7078
document.head.appendChild($link)
7179

7280
return {
7381
remove: () => {
7482
$link.parentNode && $link.parentNode.removeChild($link)
75-
cssSet.delete(href)
83+
cssMap.delete(href)
7684
},
85+
$link,
7786
}
7887
}
7988

0 commit comments

Comments
 (0)