@chakra-ui/react
Version:
Responsive and accessible React UI components built with React and Emotion
29 lines (26 loc) • 622 B
JavaScript
;
import { useState, useMemo, useEffect } from 'react';
function useCodeHighlight(props) {
const { loadContext, getHighlighter, unloadContext } = props;
const [context, setContext] = useState(null);
const highlight = useMemo(
() => getHighlighter(context),
[getHighlighter, context]
);
useEffect(() => {
let ctx = null;
loadContext?.().then((c) => {
ctx = c;
setContext(c);
});
return () => {
unloadContext?.(ctx);
};
}, [loadContext, unloadContext]);
return {
highlight,
loadContext,
getHighlighter
};
}
export { useCodeHighlight };