feat: widget link
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { createContext, useContext, ReactNode } from 'react';
|
||||
import { getContrastColor } from '../common/utils';
|
||||
|
||||
const color = decodeURIComponent(new URLSearchParams(location.search).get("themeColor") || "#1fe1f9");
|
||||
const fgColor = getContrastColor(color);
|
||||
// 判断是否是iframe上下文
|
||||
const embed = window.location !== window.parent.location;
|
||||
const WidgetContext = createContext({ color, fgColor, embed });
|
||||
|
||||
|
||||
function WidgetProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
return <WidgetContext.Provider value={{ color, fgColor, embed }} >{children}</WidgetContext.Provider>;
|
||||
}
|
||||
|
||||
function useWidget() {
|
||||
const context = useContext(WidgetContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useWidget must be used within a WidgetProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
export { WidgetProvider, useWidget };
|
||||
Reference in New Issue
Block a user