-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7721 from ethereum/ui-migrate-cardlist
UI migrate `CardList`
- Loading branch information
Showing
3 changed files
with
104 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,110 @@ | ||
import React, { ReactNode } from "react" | ||
import styled from "@emotion/styled" | ||
import { GatsbyImage } from "gatsby-plugin-image" | ||
import { | ||
Box, | ||
Flex, | ||
HStack, | ||
LinkBox, | ||
LinkOverlay, | ||
StackProps, | ||
} from "@chakra-ui/react" | ||
|
||
import Link from "./Link" | ||
import { ImageProp } from "../types" | ||
|
||
const Table = styled.div` | ||
background-color: ${(props) => props.theme.colors.background}; | ||
width: 100%; | ||
` | ||
|
||
const Item = styled.div` | ||
cursor: pointer; | ||
text-decoration: none; | ||
display: flex; | ||
justify-content: space-between; | ||
color: ${(props) => props.theme.colors.text} !important; | ||
border: 1px solid ${(props) => props.theme.colors.border}; | ||
padding: 1rem; | ||
width: 100%; | ||
color: #000000; | ||
margin-bottom: 1rem; | ||
&:hover { | ||
border-radius: 4px; | ||
box-shadow: 0 0 1px ${(props) => props.theme.colors.primary}; | ||
background: ${(props) => props.theme.colors.tableBackgroundHover}; | ||
} | ||
` | ||
|
||
const ItemLink = styled(Link)` | ||
text-decoration: none; | ||
display: flex; | ||
justify-content: space-between; | ||
color: ${(props) => props.theme.colors.text} !important; | ||
border: 1px solid ${(props) => props.theme.colors.border}; | ||
padding: 1rem; | ||
width: 100%; | ||
color: #000000; | ||
&:hover { | ||
text-decoration: none; | ||
border-radius: 4px; | ||
box-shadow: 0 0 1px ${(props) => props.theme.colors.primary}; | ||
background: ${(props) => props.theme.colors.tableBackgroundHover}; | ||
} | ||
` | ||
|
||
const ItemTitle = styled.div`` | ||
|
||
const ItemDesc = styled.div` | ||
font-size: ${(props) => props.theme.fontSizes.s}; | ||
margin-bottom: 0; | ||
opacity: 0.6; | ||
` | ||
|
||
const LeftContainer = styled.div` | ||
flex: 1 1 75%; | ||
display: flex; | ||
flex-direction: column; | ||
margin-right: 2rem; | ||
` | ||
const RightContainer = styled.div` | ||
flex: 1 0 25%; | ||
display: flex; | ||
align-items: center; | ||
margin-right: 1rem; | ||
flex-wrap: wrap; | ||
` | ||
|
||
const Image = styled(GatsbyImage)` | ||
min-width: 20px; | ||
margin-right: 1rem; | ||
margin-top: 4px; | ||
` | ||
import * as url from "../utils/url" | ||
import Link from "./Link" | ||
|
||
export type CardListItem = { | ||
title?: ReactNode | ||
description?: ReactNode | ||
caption?: ReactNode | ||
link?: string | ||
id?: string | number | ||
id?: string | ||
} & ImageProp | ||
|
||
export interface IProps { | ||
content: Array<CardListItem> | ||
className?: string | ||
clickHandler?: (idx: string | number) => void | ||
} | ||
|
||
const CardList: React.FC<IProps> = ({ | ||
content, | ||
className, | ||
clickHandler = () => null, | ||
}) => ( | ||
<Table className={className}> | ||
const CardContainer = (props: StackProps) => { | ||
return ( | ||
<HStack | ||
spacing={4} | ||
p={4} | ||
color="text" | ||
border="1px solid" | ||
borderColor="border" | ||
_hover={{ | ||
borderRadius: "base", | ||
boxShadow: "0 0 1px var(--eth-colors-primary)", | ||
background: "tableBackgroundHover", | ||
}} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
const Card = (props: CardListItem & Omit<StackProps, "title" | "id">) => { | ||
const { title, description, caption, link, image, alt, ...rest } = props | ||
|
||
const isLink = !!link | ||
const isExternal = url.isExternal(link || "") | ||
|
||
return ( | ||
<CardContainer {...rest}> | ||
{image && <Box as={GatsbyImage} image={image} alt={alt} minW="20px" />} | ||
<Flex flex="1 1 75%" direction="column"> | ||
{isLink ? ( | ||
<LinkOverlay | ||
as={Link} | ||
href={link} | ||
hideArrow | ||
color="text" | ||
textDecoration="none" | ||
_hover={{ textDecoration: "none" }} | ||
> | ||
{title} | ||
</LinkOverlay> | ||
) : ( | ||
<Box>{title}</Box> | ||
)} | ||
|
||
<Box fontSize="sm" mb={0} opacity={0.6}> | ||
{description} | ||
</Box> | ||
</Flex> | ||
{caption && ( | ||
<Flex flex="1 0 25%" align="center" wrap="wrap" mr={4}> | ||
<Box fontSize="sm" mb={0} opacity={0.6}> | ||
{caption} | ||
</Box> | ||
</Flex> | ||
)} | ||
{isExternal && <Box>↗</Box>} | ||
</CardContainer> | ||
) | ||
} | ||
|
||
const CardList: React.FC<IProps> = ({ content, clickHandler = () => null }) => ( | ||
<Box bg="background" width="full"> | ||
{content.map((listItem, idx) => { | ||
const { title, description, caption, link, image, alt, id } = listItem | ||
const { link, id } = listItem | ||
const isLink = !!link | ||
return isLink ? ( | ||
<ItemLink key={id || idx} to={link}> | ||
{image && <Image image={image} alt={alt} />} | ||
<LeftContainer> | ||
<ItemTitle>{title}</ItemTitle> | ||
|
||
<ItemDesc>{description}</ItemDesc> | ||
</LeftContainer> | ||
{caption && ( | ||
<RightContainer> | ||
<ItemDesc>{caption}</ItemDesc> | ||
</RightContainer> | ||
)} | ||
</ItemLink> | ||
return isLink ? ( | ||
<LinkBox key={id || idx}> | ||
<Card {...listItem} /> | ||
</LinkBox> | ||
) : ( | ||
<Item key={idx} onClick={() => clickHandler(idx)}> | ||
{image && <Image image={image} alt={alt} />} | ||
<LeftContainer> | ||
<ItemTitle>{title}</ItemTitle> | ||
|
||
<ItemDesc>{description}</ItemDesc> | ||
</LeftContainer> | ||
{caption && ( | ||
<RightContainer> | ||
<ItemDesc>{caption}</ItemDesc> | ||
</RightContainer> | ||
)} | ||
</Item> | ||
<Card | ||
key={idx} | ||
onClick={() => clickHandler(idx)} | ||
mb={4} | ||
{...listItem} | ||
/> | ||
) | ||
})} | ||
</Table> | ||
</Box> | ||
) | ||
|
||
export default CardList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const HASH_PATTERN = /^#.*/ | ||
const isHashLink = (href: string): boolean => HASH_PATTERN.test(href) | ||
|
||
export const isExternal = (href: string): boolean => | ||
href.includes("http") || href.includes("mailto:") | ||
|
||
export const isHash = (href: string): boolean => isHashLink(href) | ||
|
||
export const isGlossary = (href: string): boolean => | ||
href.includes("glossary") && href.includes("#") | ||
|
||
export const isStatic = (href: string): boolean => href.includes("static") | ||
|
||
export const isPdf = (href: string): boolean => href.includes(".pdf") |