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

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.
@@ -3158,17 +3158,19 @@ function Gradient() {
3158
3158
  })]
3159
3159
  });
3160
3160
  }
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";
3161
+ const ticker = "_ticker_fyd8z_9";
3162
+ const tickerItems = "_tickerItems_fyd8z_19";
3163
+ const tickerScroll = "_tickerScroll_fyd8z_24";
3164
+ const tickerItem = "_tickerItem_fyd8z_19";
3165
+ const controls = "_controls_fyd8z_46";
3166
+ const gradient = "_gradient_fyd8z_61";
3167
+ const buttons = "_buttons_fyd8z_75";
3168
+ const button = "_button_fyd8z_75";
3169
+ const buttonInner = "_buttonInner_fyd8z_101";
3169
3170
  const styles = {
3170
3171
  ticker,
3171
3172
  tickerItems,
3173
+ tickerScroll,
3172
3174
  tickerItem,
3173
3175
  controls,
3174
3176
  gradient,
@@ -3176,45 +3178,37 @@ const styles = {
3176
3178
  button,
3177
3179
  buttonInner
3178
3180
  };
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
3181
  function Ticker({
3190
3182
  maxItems = 20,
3191
3183
  onStateChange,
3192
3184
  children
3193
3185
  }) {
3194
- const [offset, setOffset] = useState(0);
3186
+ const [pageIndex, setPageIndex] = useState(0);
3187
+ const [pageWidth, setPageWidth] = useState(0);
3188
+ const [numberOfPages, setNumberOfPages] = useState(0);
3189
+ const offsetWidth = useMemo(() => {
3190
+ return -pageIndex * (pageWidth || 0);
3191
+ }, [pageIndex, pageWidth]);
3195
3192
  const windowSize = useWindowSize();
3196
3193
  const tickerRef = useRef();
3197
3194
  const tickerItemsRef = useRef();
3195
+ const tickerScrollRef = useRef();
3196
+ const controlsRef = useRef();
3198
3197
  const [hideButtons, setHideButtons] = useState(false);
3199
- const [nextButtonDisabled, setNextButtonDisabled] = useState(false);
3200
3198
  const [expanded, setExpanded] = useState(false);
3201
3199
  const childArray = toChildArray(children);
3202
3200
  useLayoutEffect(() => {
3203
3201
  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]);
3202
+ const pageWidth2 = tickerItemsContainer.clientWidth * 0.75;
3203
+ setPageWidth(pageWidth2);
3204
+ const numberOfPages2 = Math.ceil(tickerScrollRef.current.scrollWidth / pageWidth2);
3205
+ setNumberOfPages(numberOfPages2);
3206
+ }, []);
3207
+ useLayoutEffect(() => {
3208
+ const tickerItemsContainer = tickerItemsRef.current;
3209
+ const hideButtons2 = windowSize.width >= 480 ? tickerScrollRef.current.scrollWidth <= tickerItemsContainer.clientWidth : tickerScrollRef.current.scrollHeight <= tickerItemsContainer.clientHeight;
3210
+ setHideButtons(hideButtons2);
3211
+ }, [windowSize, setHideButtons]);
3218
3212
  function toggleExpandedState() {
3219
3213
  setExpanded((expanded2) => {
3220
3214
  const newState = !expanded2;
@@ -3228,32 +3222,37 @@ function Ticker({
3228
3222
  return jsxs("div", {
3229
3223
  ref: tickerRef,
3230
3224
  className: styles.ticker,
3231
- style: `--ticker-offset: ${offset}`,
3225
+ style: `--ticker-offset: ${offsetWidth}px;`,
3232
3226
  "data-expanded": expanded,
3233
3227
  children: [jsx("div", {
3234
3228
  ref: tickerItemsRef,
3235
3229
  className: styles.tickerItems,
3236
- children: childArray.map((child, index2) => jsx("div", {
3237
- className: styles.tickerItem,
3238
- children: child
3239
- }, index2))
3230
+ children: jsx("div", {
3231
+ ref: tickerScrollRef,
3232
+ className: styles.tickerScroll,
3233
+ children: childArray.map((child, index2) => jsx("div", {
3234
+ className: styles.tickerItem,
3235
+ children: child
3236
+ }, index2))
3237
+ })
3240
3238
  }), jsxs("div", {
3239
+ ref: controlsRef,
3241
3240
  className: styles.controls,
3242
3241
  style: hideButtons && {
3243
3242
  display: "none"
3244
3243
  },
3245
3244
  children: [jsx("div", {
3246
3245
  className: styles.gradient,
3247
- children: jsx(Gradient, {})
3246
+ children: !expanded && jsx(Gradient, {})
3248
3247
  }), jsxs("div", {
3249
3248
  className: styles.buttons,
3250
3249
  children: [jsx(ArrowButton, {
3251
- onClick: () => setOffset((d2) => d2 + 1),
3252
- disabled: nextButtonDisabled
3250
+ onClick: () => setPageIndex((d2) => d2 + 1),
3251
+ disabled: pageIndex >= numberOfPages - 1
3253
3252
  }), jsx(ArrowButton, {
3254
3253
  direction: "left",
3255
- onClick: () => setOffset((d2) => d2 - 1),
3256
- disabled: offset <= 0
3254
+ onClick: () => setPageIndex((d2) => d2 - 1),
3255
+ disabled: pageIndex <= 0
3257
3256
  })]
3258
3257
  }), jsx("div", {
3259
3258
  className: styles.button,