Skip to content

Commit

Permalink
feat(theming): exclude non standard properties from text styles
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed May 12, 2024
1 parent 71e70bd commit 1d4eba0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 430 deletions.
4 changes: 2 additions & 2 deletions packages/axes/src/components/AxisTick.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo, memo } from 'react'
import * as React from 'react'
import { animated } from '@react-spring/web'
import { useTheme } from '@nivo/core'
import { useTheme, sanitizeSvgTextStyle } from '@nivo/core'
import { ScaleValue } from '@nivo/scales'
import { AxisTickProps } from '../types'

Expand Down Expand Up @@ -54,7 +54,7 @@ const AxisTick = <Value extends ScaleValue>({
dominantBaseline={textBaseline}
textAnchor={textAnchor}
transform={animatedProps.textTransform}
style={textStyle}
style={sanitizeSvgTextStyle(textStyle)}
>
{`${value}`}
</animated.text>
Expand Down
2 changes: 2 additions & 0 deletions packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export type TextStyle = {
outlineOpacity: number
} & Partial<React.CSSProperties>

export function sanitizeSvgTextStyle(style: TextStyle): any

export type CompleteTheme = {
background: string
text: TextStyle
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/components/dots/DotsItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createElement, memo } from 'react'
import PropTypes from 'prop-types'
import { useSpring, animated } from '@react-spring/web'
import { useTheme } from '../../theming'
import { useTheme, sanitizeSvgTextStyle } from '../../theming'
import { useMotionConfig } from '../../motion'
import DotsItemSymbol from './DotsItemSymbol'

Expand All @@ -27,6 +27,11 @@ const DotsItem = ({
immediate: !animate,
})

console.log({
bare: theme.dots.text,
sanitized: sanitizeSvgTextStyle(theme.dots.text),
})

return (
<animated.g transform={animatedProps.transform} style={{ pointerEvents: 'none' }}>
{createElement(symbol, {
Expand All @@ -37,7 +42,11 @@ const DotsItem = ({
borderColor,
})}
{label && (
<text textAnchor={labelTextAnchor} y={labelYOffset} style={theme.dots.text}>
<text
textAnchor={labelTextAnchor}
y={labelYOffset}
style={sanitizeSvgTextStyle(theme.dots.text)}
>
{label}
</text>
)}
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/theming/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Cleanup theme text style so that all properties
* are valid for an SVG text element.
*
* @param {TextStyle} style
*/
export const sanitizeSvgTextStyle = style => {
const { outlineWidth, outlineColor, outlineOpacity, ...sanitized } = style

return sanitized
}
1 change: 1 addition & 0 deletions packages/core/src/theming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
export * from './defaultTheme'
export * from './extend'
export * from './context'
export * from './helpers'
4 changes: 2 additions & 2 deletions packages/legends/src/svg/LegendSvgItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback } from 'react'
import * as React from 'react'
import { useTheme } from '@nivo/core'
import { useTheme, sanitizeSvgTextStyle } from '@nivo/core'
import { LegendSvgItemProps } from '../types'
import { computeItemLayout } from '../compute'
import { SymbolCircle, SymbolDiamond, SymbolSquare, SymbolTriangle } from './symbols'
Expand Down Expand Up @@ -135,7 +135,7 @@ export const LegendSvgItem = ({
<text
textAnchor={labelAnchor}
style={{
...theme.legends.text,
...sanitizeSvgTextStyle(theme.legends.text),
fill: style.itemTextColor ?? textColor ?? theme.legends.text.fill ?? 'black',
dominantBaseline: labelAlignment,
pointerEvents: 'none',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ exports[`should support bottom-to-top direction 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -103,8 +101,6 @@ exports[`should support bottom-to-top direction justified 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -162,8 +158,6 @@ exports[`should support left-to-right direction 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -221,8 +215,6 @@ exports[`should support left-to-right direction justified 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -280,8 +272,6 @@ exports[`should support right-to-left direction 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -339,8 +329,6 @@ exports[`should support right-to-left direction justified 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -398,8 +386,6 @@ exports[`should support top-to-bottom direction 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down Expand Up @@ -457,8 +443,6 @@ exports[`should support top-to-bottom direction justified 1`] = `
"fill": "#333333",
"fontFamily": "sans-serif",
"fontSize": 11,
"outlineColor": "transparent",
"outlineWidth": 0,
"pointerEvents": "none",
"userSelect": "none",
}
Expand Down
Loading

0 comments on commit 1d4eba0

Please sign in to comment.