Skip to content

Commit

Permalink
fix: chart label wrong font color in dark mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxlpzqc committed Apr 26, 2024
1 parent e27afb4 commit 8b2e85e
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pages/src/components/StatusChart.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@

import { Bar, type BarConfig } from '@ant-design/charts'
import type { CountItem } from '@utils/getStatusCount'
import { useEffect, useState } from 'react'


type Props = {
data: CountItem[]
}

function useDarkMode(action: (mode: 'dark' | 'light') => void) {
const observer = new MutationObserver(mutations => {
console.log("MutationObserver", mutations)
const f = mutations.find(x => x.attributeName === 'data-theme');
if (f) {
f.target instanceof HTMLElement && action(f.target.getAttribute('data-theme') as 'dark' | 'light');
}
});
useEffect(() => {
const element = document.documentElement;
observer.observe(element, {
attributes: true,
childList: false,
subtree: false,
characterData: false,
characterDataOldValue: false,
attributeFilter: ['data-theme']
})

return () => {
observer.disconnect();
}
})
}

export default function StatusChart({ data }: Props) {

const [fontColor, setFontColor] = useState('black');

useDarkMode((mode) => {
setFontColor(mode === 'dark' ? 'white' : 'black');
});

const config: BarConfig = {
data: data.filter(x => x.status !== 'proofreading'),
xField: 'desc',
Expand All @@ -26,6 +58,7 @@ export default function StatusChart({ data }: Props) {
labelAutoHide: false,
labelFontSize: 16,
labelSpacing: 16,
labelFill: fontColor
}
},
theme: 'academy',
Expand All @@ -40,4 +73,4 @@ export default function StatusChart({ data }: Props) {
<Bar {...config}></Bar>
</div>
)
}
}

0 comments on commit 8b2e85e

Please sign in to comment.