Skip to content

Commit

Permalink
fix: correct handling of custom max-width style (#13)
Browse files Browse the repository at this point in the history
- properly applies user-defined `max-width`
- add storybook example for custom `max-width`
- add unit tests for user-defined `aspect-ratio`, `max-width`
- bump package version: `1.0.4`
  • Loading branch information
tex-murphy authored Sep 1, 2024
1 parent 1e15d0c commit 6888176
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "visionary-image",
"description": "React image component with built-in Blurhash placeholders for better UX and Core Web Vitals.",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://visionary.cloud",
"type": "module",
"main": "./dist/visionary-image.umd.js",
Expand Down
8 changes: 6 additions & 2 deletions src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,18 @@ export const Image = ({
position: "relative",
width: "100%",
};
// User wants to hardcode height/width
// Apply user-specified container width
if (userWidth) {
containerStyles.width = userWidth;
}
// Apply user-specified container height
if (userHeight) {
containerStyles.height = userHeight;
}
// Allow user to override container's max-width
if (userStyles?.maxWidth) {
containerStyles.maxWidth = userStyles.maxWidth;
}
const containerProps: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement> = {
onClick,
};
Expand All @@ -164,7 +169,6 @@ export const Image = ({
className={containerClasses}
ref={containerRef}
style={containerStyles}
// style={{ aspectRatio }}
{...getDebugIdProp(imageState.url, debug)}
{...getTestIdProp(TEST_IDS.CONTAINER)}
{...containerProps}
Expand Down
7 changes: 6 additions & 1 deletion src/components/Image/ImageWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { CSSProperties, ReactNode } from "react";

import { TEST_IDS } from "../../lib/test";
import { getTestIdProp } from "../../lib/util";

interface ImageWrapperProps {
aspectRatio: CSSProperties["aspectRatio"];
children: ReactNode;
Expand All @@ -16,5 +19,7 @@ const imageWrapperStyles: CSSProperties = {
* - Storybook example: https://visionary-ux.github.io/visionary-image/?path=/story/visionary-image--custom-aspect-ratio
*/
export const ImageWrapper = ({ aspectRatio, children }: ImageWrapperProps) => (
<div style={{ aspectRatio, ...imageWrapperStyles }}>{children}</div>
<div style={{ aspectRatio, ...imageWrapperStyles }} {...getTestIdProp(TEST_IDS.ASPECT_RATIO_WRAPPER)}>
{children}
</div>
);
21 changes: 21 additions & 0 deletions src/components/Image/__test__/Image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,27 @@ describe("Image component", () => {
});
});

describe("Style overrides", () => {
test("user can override max-width", () => {
render(<Image src={testVisionaryUrl} style={{ maxWidth: 220 }} />);

const containerElement = screen.getByTestId(TEST_IDS.CONTAINER);
const containerStyles = window.getComputedStyle(containerElement);

expect(containerStyles.maxWidth).toBe("220px");
});

test("user can specify custom aspect-ratio", () => {
const customAspectRatio = "3 / 1";
render(<Image src={testVisionaryUrl} style={{ aspectRatio: customAspectRatio }} />);

const containerElement = screen.getByTestId(TEST_IDS.ASPECT_RATIO_WRAPPER);
const containerStyles = window.getComputedStyle(containerElement);

expect(containerStyles.getPropertyValue("aspect-ratio")).toBe(customAspectRatio);
});
});

describe("Using ordinary (non-visionary) URL ", () => {
test("renders fallback <img /> element when src is not visionary", () => {
const testClassname = "v7y_tester";
Expand Down
1 change: 1 addition & 0 deletions src/lib/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const TEST_IDS = {
ASPECT_RATIO_WRAPPER: "aspect-ratio-wrapper",
CANVAS: "canvas",
CONTAINER: "container",
IMAGE: "image",
Expand Down
15 changes: 13 additions & 2 deletions src/stories/Image.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export const PreventSelection: Story = {
},
};

export const CustomSizeToken: Story = {
args: {
...sharedProps,
size: ImageSizeToken.xs,
},
};

export const CustomAspectRatio: Story = {
args: {
...sharedProps,
Expand All @@ -74,13 +81,17 @@ export const CustomAspectRatio: Story = {
transform: "translateY(-30%)",
},
},
name: "Custom aspect-ratio style",
};

export const CustomSizeToken: Story = {
export const CustomMaxWidth: Story = {
args: {
...sharedProps,
size: ImageSizeToken.xs,
style: {
maxWidth: 210,
},
},
name: "Custom max-width style",
};

export const ExternalImageUrl: Story = {
Expand Down

0 comments on commit 6888176

Please sign in to comment.