Comments (14)
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.
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.
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.
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.
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.
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.
@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.
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.
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.
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.
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.
Since next 13 support has been released. I'm closing this issue
from storybook-addon-next.
@RyanClementsHax What is the fix in Next 13?
from storybook-addon-next.
@ackvf in next 13 next/future/image just becomes next/image which this addon supports
from storybook-addon-next.
Related Issues (20)
- Does not work with `@nrwl/react/plugins/storybook` addon HOT 16
- Failed to load preset: /dist/preset.js HOT 3
- Next 12.2 newNextLinkBehavior support HOT 7
- I'm trying to use the addon with storybook and react, but can't build storybook once I installed the addon HOT 7
- Autoprefixer not working HOT 5
- Does not work with next/future/image HOT 3
- already written HOT 1
- Doesn't support AVIF format HOT 3
- Incopatibility with @storybook/builder-vite HOT 1
- Nextjs Image Error message Failed to parse src "static/media/public/logo.jpeg" on `next/image` HOT 3
- next 13 support HOT 11
- Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted HOT 4
- Enabling react strict mode with this addon causes "Rendered more hooks than during the previous render." error. HOT 1
- Examples broken on next13, node 16.17.0 HOT 2
- Allow use of ES Module for NextJS config HOT 1
- Support Storybook v7 for upgrading HOT 9
- Nextjs Image Error message Failed to parse src "static/media/public/logo.jpeg" on next/legacy/image on Nextv13 HOT 1
- Image doesn't support the `loader` prop HOT 2
- Object.defineProperty(NextImage, '__esModule', descriptor) is no more needed from [email protected] HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from storybook-addon-next.