From 6387bdcbbb4d501d2e3c967e385c670fdb60e77d Mon Sep 17 00:00:00 2001 From: Jon Kafton <939376+jonkafton@users.noreply.github.com> Date: Mon, 17 Jun 2024 19:58:04 +0200 Subject: [PATCH 1/3] Pass slot props to Card. Adjust css to align and match designs --- .../src/pages/HomePage/NewsEventsSection.tsx | 9 +- .../src/components/Card/Card.tsx | 83 +++++++++++++------ 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/frontends/mit-open/src/pages/HomePage/NewsEventsSection.tsx b/frontends/mit-open/src/pages/HomePage/NewsEventsSection.tsx index c1edbdea68..33764d53da 100644 --- a/frontends/mit-open/src/pages/HomePage/NewsEventsSection.tsx +++ b/frontends/mit-open/src/pages/HomePage/NewsEventsSection.tsx @@ -73,7 +73,6 @@ const EventsContainer = styled.section` flex-direction: column; align-items: flex-start; gap: 12px; - align-self: stretch; ` const StoryCard = styled(Card)<{ mobile: boolean }>` @@ -145,8 +144,6 @@ const EventMonth = styled.p` ` const EventTitle = styled.p` - height: ${theme.typography.pxToRem(59)}; - align-self: stretch; color: ${theme.custom.colors.darkGray2}; ${{ ...theme.typography.subtitle1 }} margin: 0; @@ -176,7 +173,9 @@ const Story: React.FC<{ item: NewsFeedItem; mobile: boolean }> = ({ return ( - {item.title} + + {item.title} + Published: {formatDate(item.news_details?.publish_date)} @@ -258,7 +257,7 @@ const NewsEventsSection: React.FC = () => { Stories - + {stories.map((item) => ( diff --git a/frontends/ol-components/src/components/Card/Card.tsx b/frontends/ol-components/src/components/Card/Card.tsx index 4d39564103..25ab02700a 100644 --- a/frontends/ol-components/src/components/Card/Card.tsx +++ b/frontends/ol-components/src/components/Card/Card.tsx @@ -4,6 +4,7 @@ import React, { Children, ImgHTMLAttributes, isValidElement, + CSSProperties, } from "react" import styled from "@emotion/styled" import { theme } from "../ThemeProvider/ThemeProvider" @@ -81,9 +82,13 @@ const Info = styled.div<{ size?: Size }>` margin-bottom: ${({ size }) => (size === "small" ? 4 : 8)}px; ` -const Title = styled.h3<{ size?: Size }>` +const Title = styled.h3<{ lines?: number; size?: Size }>` text-overflow: ellipsis; - height: ${({ size }) => theme.typography.pxToRem(size === "small" ? 36 : 60)}; + height: ${({ lines, size }) => { + const lineHeightPx = size === "small" ? 18 : 20 + lines = lines ?? (size === "small" ? 2 : 3) + return theme.typography.pxToRem(lines * lineHeightPx) + }}; overflow: hidden; margin: 0; @@ -91,12 +96,17 @@ const Title = styled.h3<{ size?: Size }>` size === "small" ? { ...theme.typography.subtitle2 } : { ...theme.typography.subtitle1 }} - @supports (-webkit-line-clamp: ${({ size }) => (size === "small" ? 2 : 3)}) { - white-space: initial; - display: -webkit-box; - -webkit-line-clamp: ${({ size }) => (size === "small" ? 2 : 3)}; - -webkit-box-orient: vertical; - } + + ${({ lines, size }) => { + lines = lines ?? (size === "small" ? 2 : 3) + return ` + @supports (-webkit-line-clamp: ${lines}) { + white-space: initial; + display: -webkit-box; + -webkit-line-clamp: ${lines}; + -webkit-box-orient: vertical; + }` + }} ` const Footer = styled.span` @@ -134,17 +144,34 @@ type CardProps = { size?: Size href?: string } + +type ImageProps = ImgHTMLAttributes & { + size?: Size + style?: CSSProperties +} +type TitleProps = { + children?: ReactNode + lines?: number + style?: CSSProperties +} +type SlotProps = { children?: ReactNode; style?: CSSProperties } + type Card = FC & { Content: FC<{ children: ReactNode }> - Image: FC | { size?: Size }> - Info: FC<{ children: ReactNode }> - Title: FC<{ children: ReactNode; size?: Size }> - Footer: FC<{ children: ReactNode }> - Actions: FC<{ children: ReactNode }> + Image: FC + Info: FC + Title: FC + Footer: FC + Actions: FC } const Card: Card = ({ children, className, size, href }) => { - let content, imageProps, info, title, footer, actions + let content, + image: ImageProps = {}, + info: SlotProps = {}, + title: TitleProps = {}, + footer: SlotProps = {}, + actions: SlotProps = {} const _Container = href ? LinkContainer : Container @@ -164,11 +191,11 @@ const Card: Card = ({ children, className, size, href }) => { Children.forEach(children, (child) => { if (!isValidElement(child)) return if (child.type === Content) content = child.props.children - else if (child.type === Image) imageProps = child.props - else if (child.type === Info) info = child.props.children - else if (child.type === Title) title = child.props.children - else if (child.type === Footer) footer = child.props.children - else if (child.type === Actions) actions = child.props.children + else if (child.type === Image) image = child.props + else if (child.type === Info) info = child.props + else if (child.type === Title) title = child.props + else if (child.type === Footer) footer = child.props + else if (child.type === Actions) actions = child.props }) if (content) { @@ -184,21 +211,27 @@ const Card: Card = ({ children, className, size, href }) => { return ( <_Container to={href!}> - {imageProps && ( + {image && ( )} + {...(image as ImgHTMLAttributes)} /> )} - {info && {info}} - {title} + {info.children && ( + + {info.children} + + )} + + {title.children} + -
{footer}
+
{footer.children}
- {actions && {actions}} + {actions.children && {actions.children}}
) } From 22692be43cc65ae8b0cbdc730f2b392fe1fc8ddf Mon Sep 17 00:00:00 2001 From: Jon Kafton <939376+jonkafton@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:23:27 +0200 Subject: [PATCH 2/3] Remove date label and abbreviate month for small variant cards --- .../LearningResourceCard/LearningResourceCard.tsx | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx b/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx index 58712a9e77..835bb655b5 100644 --- a/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx +++ b/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx @@ -73,7 +73,7 @@ const isOcw = (resource: LearningResource) => resource.resource_type === ResourceTypeEnum.Course && resource.platform?.code === PlatformEnum.Ocw -const getStartDate = (resource: LearningResource) => { +const getStartDate = (resource: LearningResource, size?: Size) => { let startDate = resource.next_start_date if (!startDate) { @@ -87,7 +87,7 @@ const getStartDate = (resource: LearningResource) => { if (!startDate) return null - return formatDate(startDate, "MMMM DD, YYYY") + return formatDate(startDate, `MMM${size === "medium" ? "M" : ""} DD, YYYY`) } const StartDate: React.FC<{ resource: LearningResource; size?: Size }> = ({ @@ -98,11 +98,8 @@ const StartDate: React.FC<{ resource: LearningResource; size?: Size }> = ({ if (!startDate) return null - const label = isOcw(resource) - ? size === "medium" - ? "As taught in:" - : "" - : "Starts:" + const label = + size === "medium" ? (isOcw(resource) ? "As taught in:" : "Starts:") : "" return ( <> From 9ac3381f19c8252ec1c5227934c200a5d34bf4b7 Mon Sep 17 00:00:00 2001 From: Jon Kafton <939376+jonkafton@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:56:22 +0200 Subject: [PATCH 3/3] Default size to medium --- .../components/LearningResourceCard/LearningResourceCard.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx b/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx index 835bb655b5..2580d476e0 100644 --- a/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx +++ b/frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx @@ -73,7 +73,7 @@ const isOcw = (resource: LearningResource) => resource.resource_type === ResourceTypeEnum.Course && resource.platform?.code === PlatformEnum.Ocw -const getStartDate = (resource: LearningResource, size?: Size) => { +const getStartDate = (resource: LearningResource, size: Size = "medium") => { let startDate = resource.next_start_date if (!startDate) {