Skip to content

Commit

Permalink
✨ a11y PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Dec 3, 2024
1 parent bc93bbe commit d761a03
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 45 deletions.
12 changes: 0 additions & 12 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2000,15 +2000,3 @@ export function getPaginationPageNumbers(

return pageNumbers
}

/**
* Convert 0 to "0th", 1 to "1st", etc.
* */
export function getOrdinalNumberString(n: number): string {
const suffix = ["th", "st", "nd", "rd"]
const modulusValue = n % 100
return (
n +
(suffix[(modulusValue - 20) % 10] || suffix[modulusValue] || suffix[0])
)
}
1 change: 0 additions & 1 deletion packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export {
formatInlineList,
lazy,
getParentVariableIdFromChartConfig,
getOrdinalNumberString,
} from "./Util.js"

export {
Expand Down
4 changes: 2 additions & 2 deletions site/gdocs/components/LatestDataInsights.scss
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ html:not(.js-enabled) .latest-data-insights__card,
}
}

.latest-data-insights__control-dots {
.latest-data-insights__dots {
position: absolute;
display: flex;
justify-content: center;
Expand All @@ -207,7 +207,7 @@ html:not(.js-enabled) .latest-data-insights__card,
}
}

.latest-data-insights__control-dot {
.latest-data-insights__dot {
background-color: $blue-30;
touch-action: manipulation;
display: inline-flex;
Expand Down
36 changes: 6 additions & 30 deletions site/gdocs/components/LatestDataInsights.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import * as React from "react"
import {
memo,
useState,
useCallback,
useEffect,
useMemo,
ComponentPropsWithRef,
} from "react"
import { memo, useState, useCallback, useEffect, useMemo } from "react"
import { useMediaQuery } from "usehooks-ts"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import {
Expand All @@ -24,7 +17,6 @@ import {
EnrichedBlockImage,
OwidEnrichedGdocBlock,
LatestDataInsight,
getOrdinalNumberString,
} from "@ourworldindata/utils"
import { dataInsightIndexToIdMap } from "../pages/DataInsight.js"
import Image from "./Image.js"
Expand Down Expand Up @@ -55,8 +47,7 @@ export default function LatestDataInsights({
const [emblaRef, emblaApi] = useEmblaCarousel({ align: "start" }, [
ClassNames(),
])
const { selectedIndex, scrollSnaps, onDotButtonClick } =
useDotButton(emblaApi)
const { selectedIndex, scrollSnaps } = useDotButton(emblaApi)
const [canScrollPrev, setCanScrollPrev] = useState(false)
const [canScrollNext, setCanScrollNext] = useState(false)

Expand Down Expand Up @@ -130,14 +121,12 @@ export default function LatestDataInsights({
text=""
/>
)}
<div className="latest-data-insights__control-dots">
<div className="latest-data-insights__dots">
{scrollSnaps.map((_, index) => (
<DotButton
<div
key={index}
index={index}
onClick={() => onDotButtonClick(index)}
className={cx("latest-data-insights__control-dot", {
"latest-data-insights__control-dot--selected":
className={cx("latest-data-insights__dot", {
"latest-data-insights__dot--selected":
index === selectedIndex,
})}
/>
Expand Down Expand Up @@ -252,16 +241,3 @@ function useDotButton(emblaApi: EmblaCarouselType | undefined): {
onDotButtonClick,
}
}

function DotButton({
children,
index,
...restProps
}: ComponentPropsWithRef<"button"> & { index: number }) {
const label = `Navigate to the ${getOrdinalNumberString(index + 1)} data insight card`
return (
<button aria-label={label} {...restProps}>
{children}
</button>
)
}

0 comments on commit d761a03

Please sign in to comment.