UNPKG

@nivo/theming

Version:
1 lines 12.8 kB
{"version":3,"file":"nivo-theming.mjs","sources":["../src/bridge.ts","../src/extend.ts","../src/defaults.ts","../src/hooks.ts","../src/context.tsx"],"sourcesContent":["import { TextStyle } from './types'\n\nexport type Engine = 'svg' | 'css' | 'canvas'\n\nexport type TextAlign = 'start' | 'center' | 'end'\nexport type TextBaseline = 'top' | 'center' | 'bottom'\n\nexport interface EngineStyleAttributesMapping {\n textAlign: Record<TextAlign, string>\n textBaseline: Record<TextBaseline, string>\n}\n\nexport const svgStyleAttributesMapping: EngineStyleAttributesMapping = {\n textAlign: {\n start: 'start',\n center: 'middle',\n end: 'end',\n },\n textBaseline: {\n top: 'text-before-edge',\n center: 'middle',\n bottom: 'text-after-edge',\n },\n}\n\nexport const cssStyleAttributesMapping: EngineStyleAttributesMapping = {\n textAlign: {\n start: 'left',\n center: 'center',\n end: 'right',\n },\n textBaseline: {\n top: 'top',\n center: 'middle',\n bottom: 'bottom',\n },\n}\n\nexport const canvasStyleAttributesMapping: EngineStyleAttributesMapping = {\n textAlign: {\n start: 'left',\n center: 'center',\n end: 'right',\n },\n textBaseline: {\n top: 'top',\n center: 'middle',\n bottom: 'bottom',\n },\n}\n\nexport const styleAttributesMapping: Record<Engine, EngineStyleAttributesMapping> = {\n svg: svgStyleAttributesMapping,\n css: cssStyleAttributesMapping,\n canvas: canvasStyleAttributesMapping,\n}\n\nexport const convertStyleAttribute = <K extends keyof EngineStyleAttributesMapping>(\n engine: Engine,\n attr: K,\n value: keyof EngineStyleAttributesMapping[K]\n) => {\n return styleAttributesMapping[engine][attr][value]\n}\n\nexport const sanitizeSvgTextStyle = (\n style: TextStyle\n): Omit<TextStyle, 'outlineWidth' | 'outlineColor' | 'outlineOpacity'> => {\n const { outlineWidth, outlineColor, outlineOpacity, ...sanitized } = style\n\n return sanitized\n}\n\nexport const sanitizeHtmlTextStyle = (\n style: TextStyle\n): Omit<TextStyle, 'outlineWidth' | 'outlineColor' | 'outlineOpacity' | 'fill'> => {\n const { fill, outlineWidth, outlineColor, outlineOpacity, ...sanitized } = style\n\n return {\n ...sanitized,\n color: fill,\n }\n}\n","import merge from 'lodash/merge.js'\nimport get from 'lodash/get.js'\nimport set from 'lodash/set.js'\nimport { ThemeWithoutInheritance, PartialTheme, Theme, TextStyle } from './types'\n\nconst textPropsWithInheritance = [\n 'axis.ticks.text',\n 'axis.legend.text',\n 'legends.title.text',\n 'legends.text',\n 'legends.ticks.text',\n 'legends.title.text',\n 'labels.text',\n 'dots.text',\n 'markers.text',\n 'annotations.text',\n]\n\nexport const inheritRootThemeText = (\n partialStyle: Partial<TextStyle>,\n rootStyle: TextStyle\n): TextStyle => ({\n ...rootStyle,\n ...partialStyle,\n})\n\nexport const extendDefaultTheme = (\n defaultTheme: ThemeWithoutInheritance,\n customTheme: PartialTheme\n) => {\n const theme = merge({}, defaultTheme, customTheme) as Theme\n\n textPropsWithInheritance.forEach(prop => {\n set(theme, prop, inheritRootThemeText(get(theme, prop), theme.text))\n })\n\n return theme\n}\n\n// We support various types of axes, top, right, bottom, polar...\n// Adding new entries for each axis type is not necessarily ideal to\n// allow the customization of the theme for a specific axis.\n// We might even support multiple axes of the same type in the future.\n// We can use this helper to extend the theme for a specific axis type,\n// the overrides being provided as a property of the axis type.\n// This helper assumes that we're extending a complete theme,\n// because it's going to be used deeper in the component tree.\nexport const extendAxisTheme = (\n axisTheme: Theme['axis'],\n overrides: PartialTheme['axis']\n): Theme['axis'] => {\n if (!overrides) return axisTheme\n return merge({}, axisTheme, overrides)\n}\n","import { ThemeWithoutInheritance } from './types'\n\nexport const defaultTheme: ThemeWithoutInheritance = {\n background: 'transparent',\n text: {\n fontFamily: 'sans-serif',\n fontSize: 11,\n fill: '#333333',\n outlineWidth: 0,\n outlineColor: '#ffffff',\n outlineOpacity: 1,\n },\n axis: {\n domain: {\n line: {\n stroke: 'transparent',\n strokeWidth: 1,\n },\n },\n ticks: {\n line: {\n stroke: '#777777',\n strokeWidth: 1,\n },\n text: {},\n },\n legend: {\n text: {\n fontSize: 12,\n },\n },\n },\n grid: {\n line: {\n stroke: '#dddddd',\n strokeWidth: 1,\n },\n },\n legends: {\n hidden: {\n symbol: {\n fill: '#333333',\n opacity: 0.6,\n },\n text: {\n fill: '#333333',\n opacity: 0.6,\n },\n },\n text: {},\n ticks: {\n line: {\n stroke: '#777777',\n strokeWidth: 1,\n },\n text: {\n fontSize: 10,\n },\n },\n title: {\n text: {},\n },\n },\n labels: {\n text: {},\n },\n markers: {\n lineColor: '#000000',\n lineStrokeWidth: 1,\n text: {},\n },\n dots: {\n text: {},\n },\n tooltip: {\n container: {\n background: 'white',\n color: 'inherit',\n fontSize: 'inherit',\n borderRadius: '2px',\n boxShadow: '0 1px 2px rgba(0, 0, 0, 0.25)',\n padding: '5px 9px',\n },\n basic: {\n whiteSpace: 'pre',\n display: 'flex',\n alignItems: 'center',\n },\n chip: {\n marginRight: 7,\n },\n table: {},\n tableCell: {\n padding: '3px 5px',\n },\n tableCellValue: {\n fontWeight: 'bold',\n },\n },\n crosshair: {\n line: {\n stroke: '#000000',\n strokeWidth: 1,\n strokeOpacity: 0.75,\n strokeDasharray: '6 6',\n },\n },\n annotations: {\n text: {\n fontSize: 13,\n outlineWidth: 2,\n outlineColor: '#ffffff',\n outlineOpacity: 1,\n },\n link: {\n stroke: '#000000',\n strokeWidth: 1,\n outlineWidth: 2,\n outlineColor: '#ffffff',\n outlineOpacity: 1,\n },\n outline: {\n fill: 'none',\n stroke: '#000000',\n strokeWidth: 2,\n outlineWidth: 2,\n outlineColor: '#ffffff',\n outlineOpacity: 1,\n },\n symbol: {\n fill: '#000000',\n outlineWidth: 2,\n outlineColor: '#ffffff',\n outlineOpacity: 1,\n },\n },\n}\n","import { useMemo } from 'react'\nimport { extendDefaultTheme, extendAxisTheme } from './extend'\nimport { defaultTheme } from './defaults'\nimport { PartialTheme, Theme } from './types'\n\nexport const usePartialTheme = (partialTheme: PartialTheme) => {\n return useMemo(() => extendDefaultTheme(defaultTheme, partialTheme), [partialTheme])\n}\n\nexport const useExtendedAxisTheme = (axisTheme: Theme['axis'], overrides: PartialTheme['axis']) => {\n return useMemo(() => extendAxisTheme(axisTheme, overrides), [axisTheme, overrides])\n}\n","import { createContext, useContext, PropsWithChildren } from 'react'\nimport { usePartialTheme } from './hooks'\nimport { PartialTheme, Theme } from './types'\n\nexport const ThemeContext = createContext<Theme | null>(null)\n\n// required to preserve equality\nconst defaultPartialTheme = {}\n\nexport const ThemeProvider = ({\n theme: partialTheme = defaultPartialTheme,\n children,\n}: PropsWithChildren<{\n theme: PartialTheme\n}>) => {\n const theme = usePartialTheme(partialTheme)\n\n return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>\n}\n\nexport const useTheme = () => {\n const theme = useContext(ThemeContext)\n if (theme === null) {\n throw new Error(\n 'Unable to find the theme, did you forget to wrap your component with ThemeProvider?'\n )\n }\n\n return theme\n}\n"],"names":["svgStyleAttributesMapping","textAlign","start","center","end","textBaseline","top","bottom","cssStyleAttributesMapping","canvasStyleAttributesMapping","styleAttributesMapping","svg","css","canvas","convertStyleAttribute","engine","attr","value","sanitizeSvgTextStyle","style","outlineWidth","outlineColor","outlineOpacity","_objectWithoutPropertiesLoose","_excluded","sanitizeHtmlTextStyle","fill","_extends","_excluded2","color","textPropsWithInheritance","inheritRootThemeText","partialStyle","rootStyle","extendDefaultTheme","defaultTheme","customTheme","theme","merge","forEach","prop","set","get","text","extendAxisTheme","axisTheme","overrides","background","fontFamily","fontSize","axis","domain","line","stroke","strokeWidth","ticks","legend","grid","legends","hidden","symbol","opacity","title","labels","markers","lineColor","lineStrokeWidth","dots","tooltip","container","borderRadius","boxShadow","padding","basic","whiteSpace","display","alignItems","chip","marginRight","table","tableCell","tableCellValue","fontWeight","crosshair","strokeOpacity","strokeDasharray","annotations","link","outline","usePartialTheme","partialTheme","useMemo","useExtendedAxisTheme","ThemeContext","createContext","defaultPartialTheme","ThemeProvider","_ref","_ref$theme","children","_jsx","Provider","useTheme","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