diff --git a/packages/wethegit-components/public/masked-edge-demo/animated-1-1.gif b/packages/wethegit-components/public/masked-edge-demo/animated-1-1.gif new file mode 100644 index 00000000..0c316468 Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/animated-1-1.gif differ diff --git a/packages/wethegit-components/public/masked-edge-demo/crumb-border-bottom.png b/packages/wethegit-components/public/masked-edge-demo/crumb-border-bottom.png new file mode 100644 index 00000000..0df64cb5 Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/crumb-border-bottom.png differ diff --git a/packages/wethegit-components/public/masked-edge-demo/crumb-border.svg b/packages/wethegit-components/public/masked-edge-demo/crumb-border.svg new file mode 100644 index 00000000..9dc9ed11 --- /dev/null +++ b/packages/wethegit-components/public/masked-edge-demo/crumb-border.svg @@ -0,0 +1,3 @@ + + + diff --git a/packages/wethegit-components/public/masked-edge-demo/edge-pattern-bite-vertical.png b/packages/wethegit-components/public/masked-edge-demo/edge-pattern-bite-vertical.png new file mode 100644 index 00000000..72fd50cc Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/edge-pattern-bite-vertical.png differ diff --git a/packages/wethegit-components/public/masked-edge-demo/edge-pattern-vertical.png b/packages/wethegit-components/public/masked-edge-demo/edge-pattern-vertical.png new file mode 100644 index 00000000..6c9deb2b Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/edge-pattern-vertical.png differ diff --git a/packages/wethegit-components/public/masked-edge-demo/edge-pattern.png b/packages/wethegit-components/public/masked-edge-demo/edge-pattern.png new file mode 100644 index 00000000..793aedbd Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/edge-pattern.png differ diff --git a/packages/wethegit-components/public/masked-edge-demo/frame-1-1.png b/packages/wethegit-components/public/masked-edge-demo/frame-1-1.png new file mode 100644 index 00000000..07e6060e Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/frame-1-1.png differ diff --git a/packages/wethegit-components/public/masked-edge-demo/frame-16-9.png b/packages/wethegit-components/public/masked-edge-demo/frame-16-9.png new file mode 100644 index 00000000..7243b767 Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/frame-16-9.png differ diff --git a/packages/wethegit-components/public/masked-edge-demo/rpg-fight-bg.gif b/packages/wethegit-components/public/masked-edge-demo/rpg-fight-bg.gif new file mode 100644 index 00000000..2f586907 Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/rpg-fight-bg.gif differ diff --git a/packages/wethegit-components/public/masked-edge-demo/water-animated.gif b/packages/wethegit-components/public/masked-edge-demo/water-animated.gif new file mode 100644 index 00000000..0e4a6d43 Binary files /dev/null and b/packages/wethegit-components/public/masked-edge-demo/water-animated.gif differ diff --git a/packages/wethegit-components/src/components/index.tsx b/packages/wethegit-components/src/components/index.tsx new file mode 100644 index 00000000..a52e9f74 --- /dev/null +++ b/packages/wethegit-components/src/components/index.tsx @@ -0,0 +1,13 @@ +export * from "./breakpoint-snipe" +export * from "./flex" +export * from "./grid-layout" +export * from "./icon" +export * from "./image-group" +export * from "./in-view" +export * from "./modal" +export * from "./navigation" +export * from "./masked-edge" +export * from "./tag" +export * from "./text" +export * from "./visually-hidden" +export * from "./visually-hidden-links" diff --git a/packages/wethegit-components/src/components/masked-edge/masked-edge.module.scss b/packages/wethegit-components/src/components/masked-edge/masked-edge.module.scss new file mode 100644 index 00000000..b9f948ef --- /dev/null +++ b/packages/wethegit-components/src/components/masked-edge/masked-edge.module.scss @@ -0,0 +1,52 @@ +.maskedEdge { + position: relative; +} + +.maskSizer { + /* For container units */ + container-type: size; + inset: 0; + pointer-events: none; + position: absolute; +} + +/* Holds the inline SVG mask without taking up space */ +.maskDefs { + height: 0; + overflow: hidden; + pointer-events: none; + position: absolute; + width: 0; +} + +.maskedContent { + position: relative; +} + +.fullFrame { + inset: 0; + overflow: hidden; + position: absolute; +} + +/* Move mask to edge */ +.edgeBottom { + transform: translateY(calc(100cqh - var(--masked-edge-size))); +} + +.edgeRight { + transform: translateX(calc(100cqw - var(--masked-edge-size))); +} + +.maskImage { + transform-box: fill-box; + transform-origin: center; +} + +.mirrorX { + transform: scaleX(-1); +} + +.mirrorY { + transform: scaleY(-1); +} diff --git a/packages/wethegit-components/src/components/masked-edge/masked-edge.stories.tsx b/packages/wethegit-components/src/components/masked-edge/masked-edge.stories.tsx new file mode 100644 index 00000000..ff68cc67 --- /dev/null +++ b/packages/wethegit-components/src/components/masked-edge/masked-edge.stories.tsx @@ -0,0 +1,201 @@ +import type { CSSProperties, ReactNode } from "react" +import type { Meta, StoryObj } from "@storybook/react" + +import { MaskedEdge } from "./masked-edge" + +const pageStyle: CSSProperties = { + background: "#e6f5ff", + backgroundImage: 'url("/masked-edge-demo/rpg-fight-bg.gif")', + backgroundSize: "cover", + display: "flex", + flexWrap: "wrap", + padding: 32, + width: "fit-content", +} + +const cardStyle: CSSProperties = { + background: "#bb3eac", + color: "#fff", + maxWidth: 460, + padding: "88px 40px", +} + +const narrowCardStyle: CSSProperties = { + ...cardStyle, + maxWidth: 560, + padding: "32px 72px", +} + +const framePanelStyle: CSSProperties = { + display: "flex", + flexDirection: "column", +} + +function CardContent() { + return ( + <> +

A masked edge

+

+ Use opaque artwork to cut out the edges of the container. +

+ + ) +} + +function Scene({ children }: { children: ReactNode }) { + return
{children}
+} + +const meta: Meta = { + component: MaskedEdge, + title: "components/masked-edge", +} + +export default meta + +type Story = StoryObj + +export const Top: Story = { + render: () => ( + + + + + + ), +} + +export const Bottom: Story = { + render: () => ( + + + + + + ), +} + +export const TopAndBottomMirror: Story = { + render: () => ( + + + + + + ), +} + +export const TopAndBottom: Story = { + render: () => ( + + + + + + ), +} + +export const Right: Story = { + render: () => ( + + + +

+ One asset set on the right and mirrored to flip it the desired direction. Repeat + is not set, therefore the masked asset will stretch. +

+
+
+ ), +} + +export const LeftAndRight: Story = { + render: () => ( + + + +

+ One asset set to the left and right, mirrored. They will stretch to the ends of + the container if repeat is not set to tile the asset. +

+
+
+ ), +} + +export const Repeat: Story = { + render: () => ( + + + +

+ A pattern set to the top and bottom edge and repeating. We can adjust the tile + size or limit to a single side. +

+
+
+ ), +} + +export const FullFrame169: Story = { + render: () => ( + +
+ +
+ +
+
+
+
+ ), +} + +export const FullFrame11: Story = { + render: () => ( + +
+ +
+ +
+
+
+
+ ), +} diff --git a/packages/wethegit-components/src/components/masked-edge/masked-edge.tsx b/packages/wethegit-components/src/components/masked-edge/masked-edge.tsx new file mode 100644 index 00000000..6d628eaa --- /dev/null +++ b/packages/wethegit-components/src/components/masked-edge/masked-edge.tsx @@ -0,0 +1,422 @@ +import { useId } from "react" +import type { ComponentPropsWithoutRef, CSSProperties, ReactNode } from "react" + +import { classnames } from "@local/utilities/classnames" + +import styles from "./masked-edge.module.scss" + +export const MASKED_EDGE_SIDES = ["top", "bottom", "left", "right"] as const + +export type MaskedEdgeSide = (typeof MASKED_EDGE_SIDES)[number] + +type MaskedEdgeOwnProps = { + /** + * Shared edge artwork. + * Applied to every side added to `sides`. + */ + image?: string + + /** + * Side-specific artwork. + * Setting one also has option to mirror to opposite side + */ + top?: string + bottom?: string + left?: string + right?: string + + /** + * Sides that receive `image`. + * Accepts `top`, `bottom`, `left`, `right`, combinations like `top bottom`, `left right` or `all`. + + * Defaults to all four sides when only `image` is provided + */ + sides?: string + + /** + * Tile the artwork instead of stretching + */ + repeat?: boolean + + /** + * Size of one tile along the edge when `repeat` is used with `mirror`. + * Defaults to `size`. Numbers = pixels + */ + tileSize?: number + + /** + * Thickness of the edge container. Numbers = pixels + */ + size?: number + + /** + * Mirror the artwork on the opposite side. + + * The `bottom` and `right` mask images get flipped so the image points inwards + */ + mirror?: boolean + + /** + * Place the `image` over a predictable `frameAspect` container instead of one per edge + */ + fullFrame?: boolean + + /** + * Aspect ratio of the container when `fullFrame` is set. + */ + frameAspect?: string + children?: ReactNode +} + +export type MaskedEdgeProps = MaskedEdgeOwnProps & + Omit, keyof MaskedEdgeOwnProps> + +type Edge = { + side: MaskedEdgeSide + src: string + mirrorAxis: MirrorAxis | null +} + +type MirrorAxis = "x" | "y" + +type EdgeMask = { + id: string + patternId: string + side: MaskedEdgeSide + src: string + mirrorAxis: MirrorAxis + repeat: boolean + tileSize: string +} + +type MaskProperties = { + style: CSSProperties | null + masks: EdgeMask[] +} + +type MaskComposite = "add" | "subtract" + +const SIDE_SET = new Set(MASKED_EDGE_SIDES) + +const SIDE_POSITION: Record = { + top: "0 0", + bottom: "0 100%", + left: "0 0", + right: "100% 0", +} + +/** + * Cut a decorative edge from a container's side. + * + * The opaque part of your image is used as a mask to allow the background to show through. + +* The image can be set per side, or one side, or one side and mirrored to the other. It can be stretched, tiled or be a full frame surrounding the container. FullFrame is good for inline autoplay videos with a shapely edge. + * + * **Note**: SVG artwork that needs to stretch should set `preserveAspectRatio="none"`. + */ +export function MaskedEdge({ + image, + top, + bottom, + left, + right, + sides, + repeat = false, + tileSize, + size = 40, + mirror = false, + fullFrame = false, + frameAspect = "16 / 9", + className, + children, + style, + ...rest +}: MaskedEdgeProps): React.JSX.Element { + const edgeSize = toLength(size, "40px") + const edgeTileSize = toLength(tileSize, edgeSize) + const idPrefix = `masked-${useId().replace(/:/g, "")}` + + const sideImages: Record = { + top, + bottom, + left, + right, + } + const suppliedSides = MASKED_EDGE_SIDES.filter((side) => Boolean(sideImages[side])) + const requestedSides = parseSides(sides) + + let activeSides: MaskedEdgeSide[] + if (requestedSides) { + activeSides = Array.from(new Set([...requestedSides, ...suppliedSides])) + } else if (image) { + activeSides = [...MASKED_EDGE_SIDES] + } else { + activeSides = suppliedSides + } + + const edges = activeSides + .map((side) => { + const src = sideImages[side] ?? image + + return src ? { side, src, mirrorAxis: mirrorAxis(side, mirror) } : null + }) + .filter((edge): edge is Edge => Boolean(edge)) + + const { style: maskStyle, masks } = makeMask({ + edges, + frameSrc: fullFrame ? image : undefined, + size: edgeSize, + repeat, + tileSize: edgeTileSize, + idPrefix, + }) + + const rootStyle = { + ...(fullFrame ? { aspectRatio: frameAspect } : null), + ...maskStyle, + ...style, + "--masked-edge-size": edgeSize, + } as CSSProperties + + return ( +
+ {masks.length > 0 && ( +
+ +
+ )} + +
+ {children} +
+
+ ) +} + +function toLength(value: number | string | undefined, fallback: string): string { + if (value === undefined || value === "") return fallback + return typeof value === "number" ? `${value}px` : value +} + +function isVerticalSide(side: MaskedEdgeSide): boolean { + return side === "left" || side === "right" +} + +function edgeContainerSize(side: MaskedEdgeSide, size: string): string { + return isVerticalSide(side) ? `${size} 100%` : `100% ${size}` +} + +function repeatLayerSize(side: MaskedEdgeSide, size: string): string { + return isVerticalSide(side) ? `${size} auto` : `auto ${size}` +} + +function parseSides(value?: string): MaskedEdgeSide[] | null { + if (value === undefined) return null + + const sideNames = value.split(/[\s,|]+/) + const found: MaskedEdgeSide[] = [] + sideNames.forEach((sideName) => { + const side = sideName.toLowerCase() + if (side === "all") { + found.push(...MASKED_EDGE_SIDES) + } else if (SIDE_SET.has(side)) { + found.push(side as MaskedEdgeSide) + } + }) + + return Array.from(new Set(found)) +} + +/** + * - bottom and right edges point inwards when mirrored + */ +function mirrorAxis(side: MaskedEdgeSide, mirror: boolean): MirrorAxis | null { + if (!mirror) return null + if (side === "bottom") return "y" + if (side === "right") return "x" + + return null +} + +function makeMask({ + edges, + frameSrc, + size, + repeat, + tileSize, + idPrefix, +}: { + edges: Edge[] + frameSrc?: string + size: string + repeat: boolean + tileSize: string + idPrefix: string +}): MaskProperties { + const images: string[] = [] + const sizes: string[] = [] + const positions: string[] = [] + const repeats: string[] = [] + const composites: MaskComposite[] = [] + const masks: EdgeMask[] = [] + + const addMaskingLayer = ( + image: string, + layerSize: string, + position: string, + layerRepeat: string, + composite: MaskComposite + ) => { + images.push(image) + sizes.push(layerSize) + positions.push(position) + repeats.push(layerRepeat) + composites.push(composite) + } + + addMaskingLayer( + "linear-gradient(#000, #000)", + "100% 100%", + "0 0", + "no-repeat", + "subtract" + ) + + if (frameSrc) { + addMaskingLayer(`url("${frameSrc}")`, "100% 100%", "0 0", "no-repeat", "add") + } else { + edges.forEach(({ side, src, mirrorAxis }) => { + if (mirrorAxis) { + const id = `${idPrefix}-${side}` + + masks.push({ + id, + patternId: `${id}-tile`, + side, + src, + mirrorAxis, + repeat, + tileSize: tileSize, + }) + + addMaskingLayer(`url(#${id})`, "100% 100%", "0 0", "no-repeat", "add") + return + } + + const vertical = isVerticalSide(side) + + addMaskingLayer( + `url("${src}")`, + repeat ? repeatLayerSize(side, size) : edgeContainerSize(side, size), + SIDE_POSITION[side], + repeat ? (vertical ? "repeat-y" : "repeat-x") : "no-repeat", + "add" + ) + }) + } + + if (images.length < 2) return { style: null, masks } + + return { + masks, + style: { + maskComposite: composites.join(", "), + maskImage: images.join(", "), + maskPosition: positions.join(", "), + maskRepeat: repeats.join(", "), + maskSize: sizes.join(", "), + }, + } +} + +function mirrorClass(mirrorAxis: MirrorAxis): string { + return mirrorAxis === "x" ? styles.mirrorX : styles.mirrorY +} + +function edgeTransformClass(side: MaskedEdgeSide): string | undefined { + if (side === "bottom") return styles.edgeBottom + if (side === "right") return styles.edgeRight + + return undefined +} diff --git a/packages/wethegit-components/src/components/masked-edge/readme.mdx b/packages/wethegit-components/src/components/masked-edge/readme.mdx new file mode 100644 index 00000000..9aa8f56c --- /dev/null +++ b/packages/wethegit-components/src/components/masked-edge/readme.mdx @@ -0,0 +1,84 @@ +import { Meta } from "@storybook/blocks" + + + +# MaskedEdge + +Use a mask to cut a decorative edge from the container's side. +Where the edge art is opaque, the container is cut out. + +## Install + +```bash +npx @wethegit/components-cli add masked-edge +``` + +## Usage + +### Top + +Pass an image to `top`, optionally update `size` to taste [default size = 40px]. + +Also works for the other 3 sides, `bottom`, `left`, `right` + +```tsx + + ... + +``` + +### Top and Bottom / Left and Right - Mirror + +Pass one `image`, define `sides`, then add `mirror` to have the opposite edge inherit and mirror the image. + +If you want the edge art to repeat, add `repeat`, then `tileSize` to control size of tiling. + +Omitting `repeat` and `tileSize` will stretch image 100% to match edge length. + +```tsx + + ... + +``` + +`sides` accepts `top bottom`, `left right`, and `all`. When `image` is provided with no defined sides, it defaults to all four sides. + +### Top and Bottom / Left and Right - Unique + +Pass an image path to `top`, `bottom`, `left`, or `right`. Can be combined or a single entry. + +```tsx + + ... + +``` + +```tsx + + ... + +``` + +### Full frame + +`fullFrame` maps your `image` over the whole `frameAspect` box (16:9 by default). + +```tsx + + + +``` + +## Notes + +- SVG artwork that needs to stretch should set `preserveAspectRatio="none"`. +- The artwork is used as a mask: opaque areas are cut away and transparent + areas stay visible. +- `mirror` is ignored by `fullFrame`. +- `size` is the thickness of each edge container.