Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎉 add reusable Button component #3254

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/@ourworldindata/components/src/Button/Button.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.owid-btn {
@include body-3-medium;
padding: 8.5px 24px;
border: 1px solid transparent;
border-radius: 0;
text-align: center;
display: block;
cursor: pointer;

svg {
font-size: 0.75rem;
}
}

.owid-btn--solid-vermillion {
background-color: $vermillion;
color: #fff;

&:hover {
background-color: $accent-vermillion;
}
}

.owid-btn--outline-vermillion {
border-color: $vermillion;
color: $vermillion;

&:hover {
border-color: $accent-vermillion;
color: $accent-vermillion;
}
}

.owid-btn--solid-blue {
background-color: $blue-60;
color: #fff;

&:hover {
background-color: $blue-90;
}
}
56 changes: 56 additions & 0 deletions packages/@ourworldindata/components/src/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from "react"
import cx from "classnames"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { IconDefinition, faArrowRight } from "@fortawesome/free-solid-svg-icons"

type ButtonCommonProps = {
text: string
className?: string
theme: "solid-vermillion" | "outline-vermillion" | "solid-blue"
/** Set to null to hide the icon */
icon?: IconDefinition | null
}

type WithHrefProps = {
href: string
onClick?: never
}

type WithOnClickProps = {
onClick: () => void
href?: never
}

export type ButtonProps =
| (ButtonCommonProps & WithHrefProps)
| (ButtonCommonProps & WithOnClickProps)

export const Button = ({
theme = "solid-vermillion",
className,
href,
onClick,
text,
icon = faArrowRight,
}: ButtonProps) => {
const classes = cx("owid-btn", `owid-btn--${theme}`, className)

if (href) {
return (
<a
className={classes}
href={href}
target="_blank"
rel="noopener noreferrer"
>
{text} {icon && <FontAwesomeIcon icon={icon} />}
</a>
)
}

return (
<button className={classes} onClick={onClick}>
{text} {icon && <FontAwesomeIcon icon={icon} />}
</button>
)
}
2 changes: 2 additions & 0 deletions packages/@ourworldindata/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export {
DATAPAGE_SOURCES_AND_PROCESSING_SECTION_ID,
REUSE_THIS_WORK_SECTION_ID,
} from "./SharedDataPageConstants.js"

export { Button } from "./Button/Button.js"
14 changes: 1 addition & 13 deletions site/gdocs/components/ExplorerTiles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,11 @@
margin: 0 0 16px 0;
}
.explorer-tiles__cta {
display: block;
padding: 8.5px 16px;
color: $vermillion;
border: 1px solid $vermillion;
border-radius: 0;
width: fit-content;
justify-self: end;
grid-row: span 2;
align-self: center;
height: 40px;
&:hover {
color: $accent-vermillion;
border-color: $accent-vermillion;
}
svg {
font-size: 0.75rem;
}

@include sm-only {
justify-self: start;
margin-top: 16px;
Expand Down
13 changes: 6 additions & 7 deletions site/gdocs/components/ExplorerTiles.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { EnrichedBlockExplorerTiles } from "@ourworldindata/types"
import { Button } from "@ourworldindata/components"
import React, { useContext } from "react"
import { useLinkedChart } from "../utils.js"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { faArrowRight } from "@fortawesome/free-solid-svg-icons"
import { DocumentContext } from "../OwidGdoc.js"
import { BAKED_BASE_URL } from "../../../settings/clientSettings.js"

Expand Down Expand Up @@ -55,13 +54,13 @@ export function ExplorerTiles({
<h2 className="h2-bold span-cols-6 span-md-12 explorer-tiles__title">
{title}
</h2>
<a
<Button
className="span-cols-4 col-start-9 span-md-cols-5 col-md-start-8 col-sm-start-1 span-sm-cols-12 body-3-medium explorer-tiles__cta"
href="/charts"
>
See all our Data Explorers{" "}
<FontAwesomeIcon icon={faArrowRight} />
</a>
text="See all our Data Explorers"
theme="outline-vermillion"
/>

<p className="body-2-regular explorer-tiles__subtitle span-cols-8 span-md-cols-7 span-sm-cols-12">
{subtitle}
</p>
Expand Down
32 changes: 0 additions & 32 deletions site/gdocs/components/HomepageIntro.scss
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,6 @@
margin-top: 0;
}

.homepage-intro__subscribe-button,
.homepage-intro__see-all-work-button,
.homepage-intro__donate-button {
display: block;
width: 100%;
border: 1px solid $vermillion;
color: $vermillion;
text-align: center;
padding: 8.5px 0;

&:hover {
border-color: $accent-vermillion;
color: $accent-vermillion;
}
}

.homepage-intro__see-all-work-button-container {
display: flex;
margin-top: 32px;
Expand All @@ -161,11 +145,6 @@
.homepage-intro__see-all-work-button {
align-self: flex-end;
width: auto;
padding: 8.5px 24px;
display: inline-block;
svg {
margin-left: 8px;
}

@include sm-only {
display: none;
Expand All @@ -181,17 +160,6 @@
}
}

.homepage-intro__donate-button {
background-color: $vermillion;
color: #fff;
border-color: none;

&:hover {
background-color: $accent-vermillion;
color: #fff;
}
}

.homepage-intro__mission-wrapper {
padding-bottom: 24px;
margin-bottom: 24px;
Expand Down
38 changes: 21 additions & 17 deletions site/gdocs/components/HomepageIntro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EnrichedBlockHomepageIntroPost,
} from "@ourworldindata/types"
import { formatAuthors, groupBy } from "@ourworldindata/utils"
import { Button } from "@ourworldindata/components"
import { useLinkedDocument } from "../utils.js"
import { DocumentContext } from "../OwidGdoc.js"
import Image from "./Image.js"
Expand Down Expand Up @@ -131,12 +132,13 @@ export function HomepageIntro({ className, featuredWork }: HomepageIntroProps) {
Read about our mission{" "}
<FontAwesomeIcon icon={faArrowRight} />
</a>
<a
<Button
href="#subscribe"
className="homepage-intro__subscribe-button body-3-medium"
>
Subscribe to our newsletter
</a>
text="Subscribe to our newsletter"
theme="outline-vermillion"
icon={null}
/>
</div>
<div className="homepage-intro__mission-wrapper body-3-medium">
<p>
Expand All @@ -148,9 +150,13 @@ export function HomepageIntro({ className, featuredWork }: HomepageIntroProps) {
accessible to everyone. Consider supporting us if you
find our work valuable.
</p>
<a className="homepage-intro__donate-button" href="/donate">
Donate to support us
</a>
<Button
className="homepage-intro__donate-button"
href="/donate"
text="Donate to support us"
theme="solid-vermillion"
icon={null}
/>
</div>
<div className="h6-black-caps">As seen on</div>
<img
Expand Down Expand Up @@ -187,23 +193,21 @@ export function HomepageIntro({ className, featuredWork }: HomepageIntroProps) {
))}
</div>
<div className="span-cols-6 homepage-intro__see-all-work-button-container">
<a
<Button
href="/latest"
className="body-3-medium homepage-intro__see-all-work-button"
>
See all our latest work
<FontAwesomeIcon icon={faArrowRight} />
</a>
text="See all our latest work"
theme="outline-vermillion"
/>
</div>
</div>
<div className="span-cols-6 span-sm-cols-12">
<a
<Button
href="/latest"
className="body-3-medium homepage-intro__see-all-work-button homepage-intro__see-all-work-button--mobile"
>
See all our latest work
<FontAwesomeIcon icon={faArrowRight} />
</a>
text="See all our latest work"
theme="outline-vermillion"
/>
</div>
</section>
</section>
Expand Down
28 changes: 1 addition & 27 deletions site/gdocs/components/KeyIndicator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,8 @@
}

.datapage-link {
@include body-3-medium;

display: block;
@include sm-up {
display: inline-flex;
align-items: center;
}

min-height: 40px;
padding: 8px 24px;
margin-top: 16px;
text-align: center;
color: $white;
background-color: $blue-60;
cursor: pointer;
border: none;
display: flex;
align-items: center;
justify-content: center;

&:hover {
background-color: $blue-90;
}

svg {
font-size: 0.75rem;
margin-left: 8px;
}
display: inline-block;
}

.datapage-link-desktop {
Expand Down
18 changes: 9 additions & 9 deletions site/gdocs/components/KeyIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import cx from "classnames"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"

Check warning on line 3 in site/gdocs/components/KeyIndicator.tsx

View workflow job for this annotation

GitHub Actions / eslint

'FontAwesomeIcon' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 3 in site/gdocs/components/KeyIndicator.tsx

View workflow job for this annotation

GitHub Actions / eslint

'FontAwesomeIcon' is defined but never used. Allowed unused vars must match /^_/u
import { faArrowRight } from "@fortawesome/free-solid-svg-icons"

Check warning on line 4 in site/gdocs/components/KeyIndicator.tsx

View workflow job for this annotation

GitHub Actions / eslint

'faArrowRight' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in site/gdocs/components/KeyIndicator.tsx

View workflow job for this annotation

GitHub Actions / eslint

'faArrowRight' is defined but never used. Allowed unused vars must match /^_/u

import {
EnrichedBlockKeyIndicator,
Expand All @@ -12,6 +12,7 @@
import Chart from "./Chart.js"
import Paragraph from "./Paragraph.js"
import { useLinkedChart, useLinkedIndicator } from "../utils.js"
import { Button } from "@ourworldindata/components"

export default function KeyIndicator({
d,
Expand Down Expand Up @@ -45,13 +46,12 @@
<Paragraph d={textBlock} key={i} />
))}
</div>
<a
<Button
className="datapage-link datapage-link-desktop"
href={linkedChart.resolvedUrl}
>
Explore and learn more about this data
<FontAwesomeIcon icon={faArrowRight} />
</a>
text="Explore and learn more about this data"
theme="solid-blue"
/>
</div>
<Chart
className="key-indicator-chart col-start-5 span-cols-8 span-sm-cols-12 margin-0"
Expand All @@ -62,12 +62,12 @@
}}
shouldOptimizeForHorizontalSpace={false}
/>
<a
<Button
className="datapage-link datapage-link-mobile col-start-1 span-cols-12"
href={linkedChart.resolvedUrl}
>
Explore and learn more about this data
</a>
text="Explore and learn more about this data"
theme="solid-blue"
/>
</div>
)
}
7 changes: 0 additions & 7 deletions site/gdocs/components/KeyIndicatorCollection.scss
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,11 @@
}

.key-indicator-collection__all-charts-button {
color: $vermillion;
border: 1px solid $vermillion;
padding: 8.5px 24px;
// needed to align the button with the header
margin-top: -16px;
align-self: center;
justify-self: end;

svg {
font-size: 0.75rem;
}

@include sm-only {
margin: 24px 16px;
justify-self: unset;
Expand Down
Loading
Loading