Skip to content

Commit

Permalink
Use one observer. Get value from --smooth-corners
Browse files Browse the repository at this point in the history
  • Loading branch information
sashachabin committed Nov 22, 2023
1 parent a863063 commit 6fe6d02
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 54 deletions.
23 changes: 9 additions & 14 deletions client/components/MainPage/Card/Card.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
z-index: 1;

display: flex;
flex-direction: column;
align-items: flex-start;

height: 100%;

--smooth-corners: 0.8;
padding: 16px;
border-radius: 16px;
background-image: var(--CardBgImage);
background-position: 100% 100%;
Expand All @@ -32,14 +37,6 @@
}
}

.CardInner {
display: flex;
flex-direction: column;
align-items: flex-start;
flex-grow: 1;
padding: 16px;
}

.Card_Public {
background-color: var(--cards-public-bg);
color: var(--cards-public-text);
Expand Down Expand Up @@ -145,7 +142,7 @@
}

@media screen and (min-width: 375px) {
.CardInner {
.Card {
padding: 16px;
}

Expand Down Expand Up @@ -186,18 +183,16 @@

@media screen and (min-width: 768px) {
.Card {
border-radius: 24px;
}

.CardInner {
padding: 32px;
border-radius: 24px;
}

.CardFooterCaption_NoSubtitle {
font-size: 48px;
}

.CardTitle {
--smooth-corners: 0.8;
border-radius: 8px;
font-size: 32px;
}
Expand All @@ -208,7 +203,7 @@
}

@media screen and (min-width: 1200px) {
.CardInner {
.Card {
padding: clamp(30px, 2vw, 40px);
}

Expand Down
33 changes: 17 additions & 16 deletions client/components/MainPage/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRef } from 'react';
import classNames from 'classnames/bind';
import t from 'utils/typograph';
import { STRAPI_URL } from 'transport-common/strapi/constants';
import { useCardSmoothCorners } from './useCardSmoothCorners';
import { useSmoothCorners } from './useSmoothCorners';

import { CardProps } from './Card.types';

Expand All @@ -29,7 +29,9 @@ export function Card({
footerCaption,
}: CardProps) {
const cardRef = useRef<HTMLAnchorElement>(null);
useCardSmoothCorners(cardRef);
const cardTitleRef = useRef<HTMLDivElement>(null);
useSmoothCorners(cardRef);
useSmoothCorners(cardTitleRef);

return (
<a
Expand All @@ -45,20 +47,19 @@ export function Card({
} as React.CSSProperties
}
>
<div className={cn(styles.CardInner)}>
{title && (
<div
className={cn(styles.CardTitle, {
[styles.CardTitle_Bg]: titleBackgroundColor,
})}
>
{t(title)}
</div>
)}
{headerCaption && <p className={cn(styles.CardHeaderCaption)}>{t(headerCaption)}</p>}
{footerCaption && <p className={headerCaption ? cn(styles.CardFooterCaption) : cn(styles.CardFooterCaption_NoSubtitle)}>{t(footerCaption)}</p>}
{dynamicContent && <div className={cn(styles.CardDynamic)}>{dynamicContent}</div>}
</div>
{title && (
<div
className={cn(styles.CardTitle, {
[styles.CardTitle_Bg]: titleBackgroundColor,
})}
ref={cardTitleRef}
>
{t(title)}
</div>
)}
{headerCaption && <p className={cn(styles.CardHeaderCaption)}>{t(headerCaption)}</p>}
{footerCaption && <p className={headerCaption ? cn(styles.CardFooterCaption) : cn(styles.CardFooterCaption_NoSubtitle)}>{t(footerCaption)}</p>}
{dynamicContent && <div className={cn(styles.CardDynamic)}>{dynamicContent}</div>}
</a>
);
}
24 changes: 0 additions & 24 deletions client/components/MainPage/Card/useCardSmoothCorners.ts

This file was deleted.

38 changes: 38 additions & 0 deletions client/components/MainPage/Card/useSmoothCorners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { MutableRefObject } from "react";
import { useEffect } from "react";
import { getSvgPath } from "figma-squircle";

function setCorners(entry: ResizeObserverEntry) {
const element = entry.target as HTMLElement;
const styles = window.getComputedStyle(element);
const { width, height, top, right, bottom, left } = entry.contentRect;

const clipPath = getSvgPath({
width: left + width + right,
height: top + height + bottom,
cornerRadius: parseInt(styles.borderRadius, 10),
cornerSmoothing: Number(styles.getPropertyValue('--smooth-corners'))
});

element.style.clipPath = `path('${clipPath}')`;
}

let smoothCornersObserver = null;

if ('ResizeObserver' in globalThis) {
smoothCornersObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
setCorners(entry);
}
});
}

export function useSmoothCorners(cardRef: MutableRefObject<HTMLElement>) {
useEffect(() => {
smoothCornersObserver.observe(cardRef.current);

return () => {
smoothCornersObserver.unobserve(cardRef.current);
};
}, []);
}

1 comment on commit 6fe6d02

@ekbdev
Copy link

@ekbdev ekbdev commented on 6fe6d02 Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for transport ready!

✅ Preview
https://transport-6mboqc4km-ekbdev.vercel.app
https://ekbdev-transport-feature-card-smooth-corners.vercel.app

Built with commit 6fe6d02.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.