Skip to content

Commit

Permalink
reuse crossfade configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian committed Feb 5, 2021
1 parent f1cd390 commit 04afe64
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 49 deletions.
18 changes: 10 additions & 8 deletions spotlight-client/src/Statistic/Statistic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ import { rem } from "polished";
import React from "react";
import { animated, useTransition } from "react-spring/web.cjs";
import styled from "styled-components/macro";
import { colors, fluidFontSizeStyles, typefaces } from "../UiLibrary";
import {
animation,
colors,
fluidFontSizeStyles,
typefaces,
} from "../UiLibrary";

const StatisticsWrapper = styled.figure``;
const StatisticsWrapper = styled.figure`
position: relative;
`;

const Value = styled.div<{ minSize: number; maxSize: number }>`
color: ${colors.text};
Expand Down Expand Up @@ -54,12 +61,7 @@ const Statistic: React.FC<StatisticProps> = ({
minSize,
value = "No data",
}) => {
const transitions = useTransition(value, null, {
initial: { opacity: 1 },
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0, position: "absolute" },
});
const transitions = useTransition(value, null, animation.crossFade);
return (
// figcaption does not seem to get consistently picked up as the accessible name,
// so including it as a label here too for insurance
Expand Down
8 changes: 8 additions & 0 deletions spotlight-client/src/UiLibrary/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
// =============================================================================

const defaultDuration = 500;
const crossFade = {
initial: { opacity: 1, top: 0 },
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0, position: "absolute" },
config: { friction: 40, tension: 280 },
} as const;

export default {
defaultDuration,
crossFade,
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import DemographicsByCategoryMetric from "../contentModels/DemographicsByCategor
import DemographicFilterSelect from "../DemographicFilterSelect";
import FiltersWrapper from "../FiltersWrapper";
import NoMetricData from "../NoMetricData";
import { zIndex } from "../UiLibrary";
import { animation, zIndex } from "../UiLibrary";

const bubbleChartHeight = 325;

Expand Down Expand Up @@ -64,13 +64,7 @@ const VizDemographicsByCategory: React.FC<VizDemographicsByCategoryProps> = ({
const chartTransitions = useTransition(
{ demographicView, dataSeries },
(item) => item.demographicView,
{
initial: { opacity: 1 },
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0, position: "absolute" },
config: { friction: 40, tension: 280 },
}
animation.crossFade
);

if (demographicView === "nofilter")
Expand All @@ -94,7 +88,7 @@ const VizDemographicsByCategory: React.FC<VizDemographicsByCategoryProps> = ({
<animated.div style={chartContainerStyles}>
{chartTransitions.map(({ item, key, props }) => (
<ChartWrapper key={key} ref={measureRef}>
<animated.div style={{ ...props, top: 0 }}>
<animated.div style={props}>
{
// for type safety we have to check this again
// but it should always be defined if we've gotten this far
Expand Down
19 changes: 7 additions & 12 deletions spotlight-client/src/VizPrisonStayLengths/VizPrisonStayLengths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import DemographicFilterSelect from "../DemographicFilterSelect";
import FiltersWrapper from "../FiltersWrapper";
import { prisonStayLengthFields } from "../metricsApi";
import NoMetricData from "../NoMetricData";
import { animation } from "../UiLibrary";

const ChartsWrapper = styled.div`
position: relative;
Expand Down Expand Up @@ -81,13 +82,7 @@ const VizPrisonStayLengths: React.FC<VizPrisonStayLengthsProps> = ({
const chartTransitions = useTransition(
{ demographicView, dataSeries },
(item) => item.demographicView,
{
initial: { opacity: 1 },
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0, position: "absolute" },
config: { friction: 40, tension: 280 },
}
animation.crossFade
);

if (demographicView === "nofilter")
Expand All @@ -109,9 +104,9 @@ const VizPrisonStayLengths: React.FC<VizPrisonStayLengthsProps> = ({
filters={[<DemographicFilterSelect metric={metric} />]}
/>
<animated.div style={chartContainerStyles}>
{chartTransitions.map(({ item, key, props }) => (
<ChartsWrapper key={key} ref={measureRef}>
<animated.div style={{ ...props, top: 0 }}>
<ChartsWrapper ref={measureRef}>
{chartTransitions.map(({ item, key, props }) => (
<animated.div key={key} style={props}>
{
// for type safety we have to check this again
// but it should always be defined if we've gotten this far
Expand All @@ -123,8 +118,8 @@ const VizPrisonStayLengths: React.FC<VizPrisonStayLengthsProps> = ({
)
}
</animated.div>
</ChartsWrapper>
))}
))}
</ChartsWrapper>
</animated.div>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import RecidivismRateMetric from "../contentModels/RecidivismRateMetric";
import DemographicFilterSelect from "../DemographicFilterSelect";
import FiltersWrapper from "../FiltersWrapper";
import NoMetricData from "../NoMetricData";
import { animation } from "../UiLibrary";
import FollowupPeriodFilterSelect from "./FollowupPeriodFilterSelect";

const ChartsWrapper = styled.div`
Expand Down Expand Up @@ -79,13 +80,7 @@ const VizRecidivismRateSingleFollowup: React.FC<VizRecidivismRateSingleFollowupP
const chartTransitions = useTransition(
{ demographicView, singleFollowupDemographics },
(item) => item.demographicView,
{
initial: { opacity: 1 },
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0, position: "absolute" },
config: { friction: 40, tension: 280 },
}
animation.crossFade
);

if (demographicView === "nofilter")
Expand All @@ -110,9 +105,9 @@ const VizRecidivismRateSingleFollowup: React.FC<VizRecidivismRateSingleFollowupP
]}
/>
<animated.div style={chartContainerStyles}>
{chartTransitions.map(({ item, key, props }) => (
<ChartsWrapper key={key} ref={measureRef}>
<animated.div style={{ ...props, top: 0 }}>
<ChartsWrapper ref={measureRef}>
{chartTransitions.map(({ item, key, props }) => (
<animated.div key={key} style={props}>
{
// for type safety we have to check this again
// but it should always be defined if we've gotten this far
Expand All @@ -125,8 +120,8 @@ const VizRecidivismRateSingleFollowup: React.FC<VizRecidivismRateSingleFollowupP
)
}
</animated.div>
</ChartsWrapper>
))}
))}
</ChartsWrapper>
</animated.div>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import DemographicFilterSelect from "../DemographicFilterSelect";
import FiltersWrapper from "../FiltersWrapper";
import LocalityFilterSelect from "../LocalityFilterSelect";
import NoMetricData from "../NoMetricData";
import { animation } from "../UiLibrary";
import SentenceTypeChart, {
CHART_BOTTOM_PADDING,
CHART_HEIGHT,
Expand Down Expand Up @@ -52,13 +53,7 @@ const VizSentenceTypeByLocation: React.FC<VizSentenceTypeByLocationProps> = ({
const chartTransitions = useTransition(
{ demographicView, dataGraph, localityId },
(item) => `${item.demographicView} ${item.localityId}`,
{
initial: { opacity: 1 },
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0, position: "absolute" },
config: { friction: 40, tension: 280 },
}
animation.crossFade
);

if (metric.dataGraph) {
Expand All @@ -74,7 +69,7 @@ const VizSentenceTypeByLocation: React.FC<VizSentenceTypeByLocationProps> = ({
{chartTransitions.map(
({ item, key, props }) =>
item.dataGraph && (
<animated.div key={key} style={{ ...props, top: 0 }}>
<animated.div key={key} style={props}>
<SentenceTypeChart {...item.dataGraph} />
</animated.div>
)
Expand Down

0 comments on commit 04afe64

Please sign in to comment.