Code Monkey home page Code Monkey logo

Comments (14)

dawidk92 avatar dawidk92 commented on July 24, 2024 4

Workaround with support of fill property from v12.2.4

// .storybook/preview.js

import * as NextFutureImage from "next/future/image";

Object.defineProperty(NextFutureImage, "default", {
  configurable: true,
  value: (props) => {
    const { fill, ...restProps } = props;

    return (
      <img
        {...restProps}
        style={
          fill
            ? {
                position: "absolute",
                height: "100%",
                width: "100%",
                inset: 0,
                color: "transparent",
              }
            : undefined
        }
      />
    );
  },
});

from storybook-addon-next.

Karibash avatar Karibash commented on July 24, 2024 3

I think this is the simplest solution.

import Image from 'next/future/image';

const OriginalImage = Image.default;
Object.defineProperty(Image, 'default', {
  configurable: true,
  value: props => <OriginalImage {...props} unoptimized />,
});

from storybook-addon-next.

kodai3 avatar kodai3 commented on July 24, 2024 1

This hack works.
But, next/future/image render only img tag, while next/image render img tag wrapped with span.
Storybook may show different results from production.

from storybook-addon-next.

CesarBenavides777 avatar CesarBenavides777 commented on July 24, 2024 1

Yeah I guess the storybook add on will need to be updated. Otherwise we'd have to style for future and non future variants which would be annoying.

from storybook-addon-next.

JVictorLourenco avatar JVictorLourenco commented on July 24, 2024 1

An addition on @dawidk92 solution:
in order to support both string paths and images object imports we can do the following:

Object.defineProperty(NextFutureImage, 'default', {
  configurable: true,
  value: (props) => {
    const { fill, src, ...restProps } = props;
    const fixedSrc = typeof src === 'object' ? src.src : src;

    return (
      <img
        {...restProps}
        src={fixedSrc}
        style={
          fill
            ? {
                position: 'absolute',
                height: '100%',
                width: '100%',
                inset: 0,
                color: 'transparent',
              }
            : undefined
        }
      />
    );
  },
});

from storybook-addon-next.

bertho-zero avatar bertho-zero commented on July 24, 2024 1

To fix the warning I have:

// .storybook/main.js

const webpack = require('webpack');
const ImageConfig = require('next/dist/shared/lib/image-config'); // <----------------

module.exports = {
  stories: [
    '../stories/**/*.stories.@(tsx|ts|jsx|js|mts|cts)',
  ],
  addons: [
    'storybook-addon-next',
  ],
  core: {
    builder: 'webpack5',
  },
  webpackFinal(config) {
    config.plugins.push(new webpack.DefinePlugin({ // <----------------
      'process.env.__NEXT_IMAGE_OPTS': JSON.stringify({
        ...ImageConfig.imageConfigDefault,
        experimentalFuture: true,
      }),
    }));

    return config;
  },
};

and

// .storybook/preview.js

import NextFutureImage from 'next/future/image';

const OriginalNextFutureImage = NextFutureImage.default;

Object.defineProperty(NextFutureImage, 'default', {
  configurable: true,
  value: props => <OriginalNextFutureImage {...props} unoptimized />,
});

from storybook-addon-next.

CesarBenavides777 avatar CesarBenavides777 commented on July 24, 2024

@kodai3 found a quick hack by using this webpack alias:

    return {
      ...config,
      resolve: {
        ...config.resolve,
        alias: {
          ...config.resolve.alias,
          "next/future/image$": "next/image",
          // styles: path.resolve(__dirname, "../src/lib/"),
        },
      },
    };

from storybook-addon-next.

dohomi avatar dohomi commented on July 24, 2024

I am looking for a solution as well, the workaround doesn't align with the same container structure so the output will different between Next and Storybook

from storybook-addon-next.

armandabric avatar armandabric commented on July 24, 2024

The fix for the fill property will be release in the next stable release: https://github.com/vercel/next.js/releases/tag/v12.2.6-canary.3

from storybook-addon-next.

mkbctrl avatar mkbctrl commented on July 24, 2024

Regarding @dawidk92 solution:
In my case the src was returned as an object, so in order to make it work I had to destructure it a bit more:

const {
      fill,
      src: { src },
      ...restProps
    } = props

    console.log(restProps)

    return (
      <img
        src={src}
        {...restProps}

from storybook-addon-next.

IdkMan2Usertive avatar IdkMan2Usertive commented on July 24, 2024

The best workaround i have made for now:

// /.storybook/preview.tsx
{
  const NextFutureImage = require('next/future/image') as typeof NextFutureImage__Type;

  const OriginalNextFutureImage = NextFutureImage.default;

  Object.defineProperty(NextFutureImage, 'default', {
    configurable: true,
    value: (props: NextFutureImageProps) => {
      const {src, ...restOfProps} = props;
      const fixedSrc: string = '/' + (typeof src === 'object' ? ('default' in src ? src.default.src : src.src) : src);
      const blurDataURL: string | undefined = props.src === 'string' ? props.src : undefined;

      return <OriginalNextFutureImage {...restOfProps} unoptimized src={fixedSrc} blurDataURL={blurDataURL} />;
    },
  });
}

This code is based on the original implementation of next/image in storybook-addon-next properly adjusted to work well with next/future/image instead.

from storybook-addon-next.

RyanClementsHax avatar RyanClementsHax commented on July 24, 2024

Since next 13 support has been released. I'm closing this issue

from storybook-addon-next.

ackvf avatar ackvf commented on July 24, 2024

@RyanClementsHax What is the fix in Next 13?

from storybook-addon-next.

RyanClementsHax avatar RyanClementsHax commented on July 24, 2024

@ackvf in next 13 next/future/image just becomes next/image which this addon supports

from storybook-addon-next.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.