Skip to content

Commit

Permalink
use hooks in our own code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed Apr 11, 2024
1 parent 4e82d96 commit 91e857a
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/x-charts/src/ChartsClipPath/ChartsClipPath.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { DrawingContext } from '../context/DrawingProvider';
import { useDrawingArea } from '../hooks/useDrawingArea';

export type ChartsClipPathProps = {
id: string;
Expand All @@ -14,7 +14,7 @@ export type ChartsClipPathProps = {
*/
function ChartsClipPath(props: ChartsClipPathProps) {
const { id, offset: offsetProps } = props;
const { left, top, width, height } = React.useContext(DrawingContext);
const { left, top, width, height } = useDrawingArea();

const offset = { top: 0, right: 0, bottom: 0, left: 0, ...offsetProps };
return (
Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/ChartsLegend/ChartsLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import PropTypes from 'prop-types';
import { useSlotProps } from '@mui/base/utils';
import { unstable_composeClasses as composeClasses } from '@mui/utils';
import { useThemeProps, useTheme, Theme } from '@mui/material/styles';
import { DrawingContext } from '../context/DrawingProvider';
import { AnchorPosition, Direction, getSeriesToDisplay } from './utils';
import { SeriesContext } from '../context/SeriesContextProvider';
import { ChartsLegendClasses, getLegendUtilityClass } from './chartsLegendClasses';
import { DefaultizedProps } from '../models/helpers';
import { DefaultChartsLegend, LegendRendererProps } from './DefaultChartsLegend';
import { useDrawingArea } from '../hooks';

export interface ChartsLegendSlots {
/**
Expand Down Expand Up @@ -82,7 +82,7 @@ function ChartsLegend(inProps: ChartsLegendProps) {
const theme = useTheme();
const classes = useUtilityClasses({ ...props, theme });

const drawingArea = React.useContext(DrawingContext);
const drawingArea = useDrawingArea();
const series = React.useContext(SeriesContext);

const seriesToDisplay = getSeriesToDisplay(series);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Delaunay } from 'd3-delaunay';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
import { InteractionContext } from '../context/InteractionProvider';
import { CartesianContext } from '../context/CartesianContextProvider';
import { SvgContext, DrawingContext } from '../context/DrawingProvider';
import { SeriesContext } from '../context/SeriesContextProvider';
import { getValueToPositionMapper } from '../hooks/useScale';
import { getSVGPoint } from '../internals/utils';
import { ScatterItemIdentifier } from '../models';
import { SeriesId } from '../models/seriesType/common';
import { useSvgRef } from '../hooks';

export type ChartsVoronoiHandlerProps = {
/**
Expand All @@ -29,8 +29,8 @@ type VoronoiSeries = { seriesId: SeriesId; startIndex: number; endIndex: number

function ChartsVoronoiHandler(props: ChartsVoronoiHandlerProps) {
const { voronoiMaxRadius, onItemClick } = props;
const svgRef = React.useContext(SvgContext);
const { width, height, top, left } = React.useContext(DrawingContext);
const svgRef = useSvgRef();
const { left, top, width, height } = useDrawingArea();
const { xAxis, yAxis, xAxisIds, yAxisIds } = React.useContext(CartesianContext);
const { dispatch } = React.useContext(InteractionContext);

Expand Down
5 changes: 2 additions & 3 deletions packages/x-charts/src/ChartsXAxis/ChartsXAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { useSlotProps } from '@mui/base/utils';
import { unstable_composeClasses as composeClasses } from '@mui/utils';
import { useThemeProps, useTheme, Theme } from '@mui/material/styles';
import { CartesianContext } from '../context/CartesianContextProvider';
import { DrawingContext } from '../context/DrawingProvider';
import { useTicks, TickItemType } from '../hooks/useTicks';
import { AxisDefaultized, ChartsXAxisProps } from '../models/axis';
import { getAxisUtilityClass } from '../ChartsAxis/axisClasses';
import { AxisRoot } from '../internals/components/AxisSharedComponents';
import { ChartsText, ChartsTextProps } from '../ChartsText';
import { getMinXTranslation } from '../internals/geometry';
import { useMounted } from '../hooks/useMounted';
import { useDrawingArea } from '../hooks/useDrawingArea';
import { getWordsByLines } from '../internals/getWordsByLines';

const useUtilityClasses = (ownerState: ChartsXAxisProps & { theme: Theme }) => {
Expand Down Expand Up @@ -130,8 +130,7 @@ function ChartsXAxis(inProps: ChartsXAxisProps) {

const theme = useTheme();
const classes = useUtilityClasses({ ...defaultizedProps, theme });

const { left, top, width, height } = React.useContext(DrawingContext);
const { left, top, width, height } = useDrawingArea();

const tickSize = disableTicks ? 4 : tickSizeProp;

Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/ChartsYAxis/ChartsYAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useSlotProps } from '@mui/base/utils';
import { unstable_composeClasses as composeClasses } from '@mui/utils';
import { useThemeProps, useTheme, Theme } from '@mui/material/styles';
import { CartesianContext } from '../context/CartesianContextProvider';
import { DrawingContext } from '../context/DrawingProvider';
import { useTicks } from '../hooks/useTicks';
import { useDrawingArea } from '../hooks/useDrawingArea';
import { ChartsYAxisProps } from '../models/axis';
import { AxisRoot } from '../internals/components/AxisSharedComponents';
import { ChartsText, ChartsTextProps } from '../ChartsText';
Expand Down Expand Up @@ -74,7 +74,7 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
const theme = useTheme();
const classes = useUtilityClasses({ ...defaultizedProps, theme });

const { left, top, width, height } = React.useContext(DrawingContext);
const { left, top, width, height } = useDrawingArea();

const tickSize = disableTicks ? 4 : tickSizeProp;

Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/Gauge/GaugeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ignore - do not document.
import * as React from 'react';
import { DrawingContext } from '../context/DrawingProvider';
import { getPercentageValue } from '../internals/utils';
import { getArcRatios, getAvailableRadius } from './utils';
import { useDrawingArea } from '../hooks/useDrawingArea';

interface CircularConfig {
/**
Expand Down Expand Up @@ -148,7 +148,7 @@ export function GaugeProvider(props: GaugeProviderProps) {
children,
} = props;

const { width, height, top, left } = React.useContext(DrawingContext);
const { left, top, width, height } = useDrawingArea();

const ratios = getArcRatios(startAngle, endAngle);

Expand Down
5 changes: 3 additions & 2 deletions packages/x-charts/src/LineChart/AnimatedArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { styled } from '@mui/material/styles';
import { color as d3Color } from 'd3-color';
import { animated, useSpring } from '@react-spring/web';
import { useAnimatedPath } from '../internals/useAnimatedPath';
import { DrawingContext } from '../context/DrawingProvider';
import { cleanId } from '../internals/utils';
import type { AreaElementOwnerState } from './AreaElement';
import { useDrawingArea } from '../hooks';

export const AreaElementPath = styled(animated.path, {
name: 'MuiAreaElement',
Expand Down Expand Up @@ -43,7 +43,8 @@ export interface AnimatedAreaProps extends React.ComponentPropsWithoutRef<'path'
*/
function AnimatedArea(props: AnimatedAreaProps) {
const { d, skipAnimation, ownerState, ...other } = props;
const { left, top, right, bottom, width, height, chartId } = React.useContext(DrawingContext);
const { left, top, right, bottom, width, height } = useDrawingArea();
const chartId = useChartId();

const path = useAnimatedPath(d!, skipAnimation);

Expand Down
6 changes: 4 additions & 2 deletions packages/x-charts/src/LineChart/AnimatedLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { animated, useSpring } from '@react-spring/web';
import { color as d3Color } from 'd3-color';
import { styled } from '@mui/material/styles';
import { useAnimatedPath } from '../internals/useAnimatedPath';
import { DrawingContext } from '../context/DrawingProvider';
import { cleanId } from '../internals/utils';
import type { LineElementOwnerState } from './LineElement';
import { useChartId } from '../hooks/useChartId';
import { useDrawingArea } from '../hooks/useDrawingArea';

export const LineElementPath = styled(animated.path, {
name: 'MuiLineElement',
Expand Down Expand Up @@ -45,7 +46,8 @@ export interface AnimatedLineProps extends React.ComponentPropsWithoutRef<'path'
*/
function AnimatedLine(props: AnimatedLineProps) {
const { d, skipAnimation, ownerState, ...other } = props;
const { left, top, bottom, width, height, right, chartId } = React.useContext(DrawingContext);
const { left, top, bottom, width, height, right } = useDrawingArea();
const chartId = useChartId();

const path = useAnimatedPath(d, skipAnimation);

Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/LineChart/MarkPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { SeriesContext } from '../context/SeriesContextProvider';
import { CartesianContext } from '../context/CartesianContextProvider';
import { MarkElement, MarkElementProps } from './MarkElement';
import { getValueToPositionMapper } from '../hooks/useScale';
import { useChartId } from '../hooks/useChartId';
import { DEFAULT_X_AXIS_KEY } from '../constants';
import { LineItemIdentifier } from '../models/seriesType/line';
import { DrawingContext } from '../context/DrawingProvider';
import { cleanId } from '../internals/utils';

export interface MarkPlotSlots {
Expand Down Expand Up @@ -56,7 +56,7 @@ function MarkPlot(props: MarkPlotProps) {

const seriesData = React.useContext(SeriesContext).line;
const axisData = React.useContext(CartesianContext);
const { chartId } = React.useContext(DrawingContext);
const chartId = useChartId();

const Mark = slots?.mark ?? MarkElement;

Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/context/CartesianContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '../LineChart/extremums';
import { AxisConfig, AxisDefaultized, isBandScaleConfig, isPointScaleConfig } from '../models/axis';
import { getScale } from '../internals/getScale';
import { DrawingContext } from './DrawingProvider';
import { SeriesContext } from './SeriesContextProvider';
import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants';
import {
Expand All @@ -26,6 +25,7 @@ import {
} from '../models/seriesType/config';
import { MakeOptional } from '../models/helpers';
import { getTickNumber } from '../hooks/useTicks';
import { useDrawingArea } from '../hooks/useDrawingArea';
import { SeriesId } from '../models/seriesType/common';

export type CartesianContextProviderProps = {
Expand Down Expand Up @@ -97,7 +97,7 @@ if (process.env.NODE_ENV !== 'production') {
function CartesianContextProvider(props: CartesianContextProviderProps) {
const { xAxis: inXAxis, yAxis: inYAxis, dataset, children } = props;
const formattedSeries = React.useContext(SeriesContext);
const drawingArea = React.useContext(DrawingContext);
const drawingArea = useDrawingArea();

const xAxis = React.useMemo(
() =>
Expand Down
1 change: 1 addition & 0 deletions packages/x-charts/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './useDrawingArea';
export * from './useChartId';
export * from './useScale';
export * from './useSvgRef';
export { useSeries as unstable_useSeries } from './useSeries';
7 changes: 4 additions & 3 deletions packages/x-charts/src/hooks/useAxisEvents.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import * as React from 'react';
import { InteractionContext } from '../context/InteractionProvider';
import { CartesianContext } from '../context/CartesianContextProvider';
import { SvgContext, DrawingContext } from '../context/DrawingProvider';
import { isBandScale } from '../internals/isBandScale';
import { AxisDefaultized } from '../models/axis';
import { getSVGPoint } from '../internals/utils';
import { useSvgRef } from './useSvgRef';
import { useDrawingArea } from './useDrawingArea';

function getAsANumber(value: number | Date) {
return value instanceof Date ? value.getTime() : value;
}
export const useAxisEvents = (disableAxisListener: boolean) => {
const svgRef = React.useContext(SvgContext);
const { width, height, top, left } = React.useContext(DrawingContext);
const svgRef = useSvgRef();
const { left, top, width, height } = useDrawingArea();
const { xAxis, yAxis, xAxisIds, yAxisIds } = React.useContext(CartesianContext);
const { dispatch } = React.useContext(InteractionContext);

Expand Down
8 changes: 8 additions & 0 deletions packages/x-charts/src/hooks/useChartId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react';
import { DrawingContext } from '../context/DrawingProvider';

export function useChartId() {
const { chartId } = React.useContext(DrawingContext);

return React.useMemo(() => chartId, [chartId]);
}
1 change: 1 addition & 0 deletions scripts/x-charts.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
{ "name": "StackOffsetType", "kind": "TypeAlias" },
{ "name": "StackOrderType", "kind": "TypeAlias" },
{ "name": "unstable_useSeries", "kind": "Function" },
{ "name": "useChartId", "kind": "Function" },
{ "name": "useDrawingArea", "kind": "Function" },
{ "name": "useGaugeState", "kind": "Function" },
{ "name": "useSvgRef", "kind": "Function" },
Expand Down

0 comments on commit 91e857a

Please sign in to comment.