Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nextjs images in storybook #684

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions services/web-app/.storybook/NextImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright IBM Corp. 2022, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import clsx from 'clsx'

// `node_modules` import is necessary to not create a cyclically resolving alias
import Image from '../../../node_modules/next/image'
Copy link
Member Author

Choose a reason for hiding this comment

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

@jdharvey-ibm this isn't pretty but I don't know a better way.

import styles from './next-image.module.scss'

const NextImage = (props) => {
const modifiedProps = { ...props, layout: 'fill' }

delete modifiedProps.className
delete modifiedProps.placeholder

return (
<div className={clsx(styles['image-container'], props.className)}>
<Image {...modifiedProps} className={styles.image} unoptimized />
</div>
)
}

export default NextImage
3 changes: 3 additions & 0 deletions services/web-app/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ module.exports = withYaml({
include: path.resolve(__dirname, '..')
})

// Use custom unoptimized image instead of Next.js Image
config.resolve.alias['next/image'] = require.resolve('./NextImage.js')

return config
}
})
23 changes: 0 additions & 23 deletions services/web-app/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,7 @@ import '../styles/styles.scss'

import { breakpoints } from '@carbon/layout'
import { Theme } from '@carbon/react'
import clsx from 'clsx'
import { RouterContext } from 'next/dist/shared/lib/router-context'
import * as NextImage from 'next/image'

import styles from './styles.module.scss'

const OriginalNextImage = NextImage.default

// eslint-disable-next-line no-import-assign -- Forcefully disable Next image optimization
Object.defineProperty(NextImage, 'default', {
configurable: true,
value: (props) => {
const modifiedProps = { ...props, layout: 'fill' }

delete modifiedProps.className
delete modifiedProps.placeholder

return (
<div className={clsx(styles['image-container'], props.className)}>
<OriginalNextImage {...modifiedProps} className={styles.image} unoptimized />
</div>
)
}
})

export const decorators = [
(Story, context) => {
Expand Down