@guardian/interactive-component-library 0.1.0-alpha.16 → 0.1.0-alpha.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { jsx, jsxs, Fragment } from "preact/jsx-runtime";
2
- import { useRef, useState, useLayoutEffect, useCallback, useEffect, useMemo, useContext, useImperativeHandle } from "preact/hooks";
2
+ import { useRef, useCallback, useEffect, useMemo, useState, useLayoutEffect, useContext, useImperativeHandle } from "preact/hooks";
3
3
  import { render, createElement, cloneElement, createContext, toChildArray } from "preact";
4
4
  import { useSyncExternalStore, createPortal, forwardRef } from "preact/compat";
5
5
  import { scaleLinear as scaleLinear$1 } from "d3-scale";
@@ -112,25 +112,6 @@ const LegendItem = ({
112
112
  })]
113
113
  });
114
114
  };
115
- function isDarkColor(color) {
116
- let r, g, b;
117
- if (color.match(/^rgb/)) {
118
- color = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/);
119
- r = color[1];
120
- g = color[2];
121
- b = color[3];
122
- } else {
123
- color = +`0x${color.slice(1).replace(color.length < 5 && /./g, "$&$&")}`;
124
- r = color >> 16;
125
- g = color >> 8 & 255;
126
- b = color & 255;
127
- }
128
- const hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b));
129
- if (hsp > 140) {
130
- return false;
131
- }
132
- return true;
133
- }
134
115
  const bar$2 = "_bar_wmb0k_1";
135
116
  const label$1 = "_label_wmb0k_5";
136
117
  const backgroundRect = "_backgroundRect_wmb0k_11";
@@ -143,30 +124,13 @@ function StackedBar({
143
124
  stack,
144
125
  width,
145
126
  height,
127
+ hideLabels = true,
146
128
  showBackgroundRect = false,
147
129
  createSVG = true,
148
130
  styles: styles2
149
131
  }) {
150
132
  const rectElements = useRef([]);
151
133
  const textElements = useRef([]);
152
- const [hideLabels, setHideLabels] = useState(true);
153
- useLayoutEffect(() => {
154
- for (let index2 = 0; index2 < stack.length; index2++) {
155
- const rectElement = rectElements.current[index2];
156
- const textElement = textElements.current[index2];
157
- const paddingX = 8;
158
- const hideText = textElement.getBBox().width + paddingX > rectElement.getBBox().width;
159
- const rectStyle = window.getComputedStyle(rectElement);
160
- const rectColor = rectStyle.getPropertyValue("fill");
161
- const textColor = isDarkColor(rectColor) ? "#FFF" : "#121212";
162
- textElement.style.fill = textColor;
163
- if (hideText) {
164
- setHideLabels(true);
165
- return;
166
- }
167
- }
168
- setHideLabels(false);
169
- }, [stack, width, height]);
170
134
  styles2 = mergeStyles({
171
135
  ...defaultStyles$q
172
136
  }, styles2);
@@ -184,7 +148,7 @@ function StackedBar({
184
148
  fill: d2.fill
185
149
  },
186
150
  "shape-rendering": "crispEdges"
187
- }), jsx("text", {
151
+ }), !hideLabels && jsx("text", {
188
152
  ref: (element) => textElements.current[index2] = element,
189
153
  x: itemWidth - 4,
190
154
  y: height / 2,
@@ -193,7 +157,7 @@ function StackedBar({
193
157
  "text-rendering": "optimizeLegibility",
194
158
  className: styles2.label,
195
159
  style: {
196
- visibility: hideLabels ? "hidden" : "visible"
160
+ display: hideLabels ? "hidden" : "visible"
197
161
  },
198
162
  children: d2.label
199
163
  })]
@@ -1772,7 +1736,7 @@ const ControlChange = ({
1772
1736
  }
1773
1737
  }) : jsx(CircleIcon, {
1774
1738
  styles: {
1775
- circle: styles2.next || `fill-color--${next2}`
1739
+ circle: `fill-color--${next2}`
1776
1740
  }
1777
1741
  }), jsx("strong", {
1778
1742
  className: styles2.text,
@@ -3158,17 +3122,19 @@ function Gradient() {
3158
3122
  })]
3159
3123
  });
3160
3124
  }
3161
- const ticker = "_ticker_1tpek_9";
3162
- const tickerItems = "_tickerItems_1tpek_19";
3163
- const tickerItem = "_tickerItem_1tpek_19";
3164
- const controls = "_controls_1tpek_41";
3165
- const gradient = "_gradient_1tpek_56";
3166
- const buttons = "_buttons_1tpek_70";
3167
- const button = "_button_1tpek_70";
3168
- const buttonInner = "_buttonInner_1tpek_96";
3125
+ const ticker = "_ticker_2eemh_9";
3126
+ const tickerItems = "_tickerItems_2eemh_19";
3127
+ const tickerScroll = "_tickerScroll_2eemh_24";
3128
+ const tickerItem = "_tickerItem_2eemh_19";
3129
+ const controls = "_controls_2eemh_46";
3130
+ const gradient = "_gradient_2eemh_61";
3131
+ const buttons = "_buttons_2eemh_75";
3132
+ const button = "_button_2eemh_75";
3133
+ const buttonInner = "_buttonInner_2eemh_101";
3169
3134
  const styles = {
3170
3135
  ticker,
3171
3136
  tickerItems,
3137
+ tickerScroll,
3172
3138
  tickerItem,
3173
3139
  controls,
3174
3140
  gradient,
@@ -3176,45 +3142,37 @@ const styles = {
3176
3142
  button,
3177
3143
  buttonInner
3178
3144
  };
3179
- function totalSizeForElements(elements) {
3180
- return Array.from(elements).reduce((totalSize, element) => {
3181
- totalSize.width += element.clientWidth;
3182
- totalSize.height += element.clientHeight;
3183
- return totalSize;
3184
- }, {
3185
- width: 0,
3186
- height: 0
3187
- });
3188
- }
3189
3145
  function Ticker({
3190
3146
  maxItems = 20,
3191
3147
  onStateChange,
3192
3148
  children
3193
3149
  }) {
3194
- const [offset, setOffset] = useState(0);
3150
+ const [pageIndex, setPageIndex] = useState(0);
3151
+ const [pageWidth, setPageWidth] = useState(0);
3152
+ const [numberOfPages, setNumberOfPages] = useState(0);
3153
+ const offsetWidth = useMemo(() => {
3154
+ return -pageIndex * (pageWidth || 0);
3155
+ }, [pageIndex, pageWidth]);
3195
3156
  const windowSize = useWindowSize();
3196
3157
  const tickerRef = useRef();
3197
3158
  const tickerItemsRef = useRef();
3159
+ const tickerScrollRef = useRef();
3160
+ const controlsRef = useRef();
3198
3161
  const [hideButtons, setHideButtons] = useState(false);
3199
- const [nextButtonDisabled, setNextButtonDisabled] = useState(false);
3200
3162
  const [expanded, setExpanded] = useState(false);
3201
3163
  const childArray = toChildArray(children);
3202
3164
  useLayoutEffect(() => {
3203
3165
  const tickerItemsContainer = tickerItemsRef.current;
3204
- if (tickerItemsContainer) {
3205
- const {
3206
- width,
3207
- height
3208
- } = totalSizeForElements(tickerItemsContainer.children);
3209
- const hideButtons2 = windowSize.width >= 480 ? width <= tickerItemsContainer.clientWidth : height <= tickerItemsContainer.clientHeight;
3210
- setHideButtons(hideButtons2);
3211
- if (tickerItemsContainer.children.length > 0) {
3212
- const itemWidth = tickerItemsContainer.children[0].clientWidth;
3213
- const nextButtonDisabled2 = offset * itemWidth > width - tickerItemsContainer.clientWidth;
3214
- setNextButtonDisabled(nextButtonDisabled2);
3215
- }
3216
- }
3217
- }, [offset, windowSize, childArray, setHideButtons, setNextButtonDisabled]);
3166
+ const pageWidth2 = tickerItemsContainer.clientWidth * 0.75;
3167
+ setPageWidth(pageWidth2);
3168
+ const numberOfPages2 = Math.ceil(tickerScrollRef.current.scrollWidth / pageWidth2);
3169
+ setNumberOfPages(numberOfPages2);
3170
+ }, []);
3171
+ useLayoutEffect(() => {
3172
+ const tickerItemsContainer = tickerItemsRef.current;
3173
+ const hideButtons2 = windowSize.width >= 480 ? tickerScrollRef.current.scrollWidth <= tickerItemsContainer.clientWidth : tickerScrollRef.current.scrollHeight <= tickerItemsContainer.clientHeight;
3174
+ setHideButtons(hideButtons2);
3175
+ }, [windowSize, setHideButtons]);
3218
3176
  function toggleExpandedState() {
3219
3177
  setExpanded((expanded2) => {
3220
3178
  const newState = !expanded2;
@@ -3228,16 +3186,21 @@ function Ticker({
3228
3186
  return jsxs("div", {
3229
3187
  ref: tickerRef,
3230
3188
  className: styles.ticker,
3231
- style: `--ticker-offset: ${offset}`,
3189
+ style: `--ticker-offset: ${offsetWidth}px;`,
3232
3190
  "data-expanded": expanded,
3233
3191
  children: [jsx("div", {
3234
3192
  ref: tickerItemsRef,
3235
3193
  className: styles.tickerItems,
3236
- children: childArray.map((child, index2) => jsx("div", {
3237
- className: styles.tickerItem,
3238
- children: child
3239
- }, index2))
3194
+ children: jsx("div", {
3195
+ ref: tickerScrollRef,
3196
+ className: styles.tickerScroll,
3197
+ children: childArray.map((child, index2) => jsx("div", {
3198
+ className: styles.tickerItem,
3199
+ children: child
3200
+ }, index2))
3201
+ })
3240
3202
  }), jsxs("div", {
3203
+ ref: controlsRef,
3241
3204
  className: styles.controls,
3242
3205
  style: hideButtons && {
3243
3206
  display: "none"
@@ -3248,12 +3211,12 @@ function Ticker({
3248
3211
  }), jsxs("div", {
3249
3212
  className: styles.buttons,
3250
3213
  children: [jsx(ArrowButton, {
3251
- onClick: () => setOffset((d2) => d2 + 1),
3252
- disabled: nextButtonDisabled
3214
+ onClick: () => setPageIndex((d2) => d2 + 1),
3215
+ disabled: pageIndex >= numberOfPages - 1
3253
3216
  }), jsx(ArrowButton, {
3254
3217
  direction: "left",
3255
- onClick: () => setOffset((d2) => d2 - 1),
3256
- disabled: offset <= 0
3218
+ onClick: () => setPageIndex((d2) => d2 - 1),
3219
+ disabled: pageIndex <= 0
3257
3220
  })]
3258
3221
  }), jsx("div", {
3259
3222
  className: styles.button,
@@ -3263,7 +3226,7 @@ function Ticker({
3263
3226
  buttonInner: styles.buttonInner
3264
3227
  },
3265
3228
  onClick: toggleExpandedState,
3266
- children: expanded ? "Show less" : `Show ${maxItems} most recent`
3229
+ children: expanded ? "Show fewer" : `Show ${maxItems} most recent`
3267
3230
  })
3268
3231
  })]
3269
3232
  })]