-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add img component for light and dark src
- Loading branch information
1 parent
02d5b6b
commit 609cb2e
Showing
8 changed files
with
176 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { type Meta, type StoryObj } from '@storybook/react' | ||
|
||
import orgSecretDark from 'assets/onboarding/org_secret_dark.png' | ||
import orgSecretLight from 'assets/onboarding/org_secret_light.png' | ||
import { | ||
Theme, | ||
ThemeContextProvider, | ||
useThemeContext, | ||
} from 'shared/ThemeContext' | ||
|
||
import LightDarkImg, { type LightDarkImgSrcProps } from './LightDarkImg' | ||
|
||
const meta: Meta<typeof LightDarkImg> = { | ||
title: 'Components/LightDarkImg', | ||
component: LightDarkImg, | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof LightDarkImg> | ||
|
||
const LightDarkImgElements = (args: LightDarkImgSrcProps) => { | ||
const { theme, setTheme } = useThemeContext() | ||
return ( | ||
<div> | ||
<button | ||
onClick={() => | ||
setTheme(theme === Theme.LIGHT ? Theme.DARK : Theme.LIGHT) | ||
} | ||
style={{ | ||
marginTop: '20px', | ||
padding: '10px 20px', | ||
backgroundColor: theme === 'light' ? '#000' : '#fff', | ||
color: theme === 'light' ? '#fff' : '#000', | ||
border: 'none', | ||
borderRadius: '4px', | ||
}} | ||
> | ||
Toggle Theme | ||
</button> | ||
<div>Current Theme: {theme}</div> | ||
<LightDarkImg {...args} /> | ||
</div> | ||
) | ||
} | ||
|
||
export const SimpleLightDarkImg: Story = { | ||
args: { | ||
alt: 'test text', | ||
src: orgSecretLight, | ||
darkSrc: orgSecretDark, | ||
}, | ||
render: (args) => ( | ||
<ThemeContextProvider> | ||
<LightDarkImgElements {...args} /> | ||
</ThemeContextProvider> | ||
), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { Theme, ThemeContext } from 'shared/ThemeContext/ThemeContext' | ||
|
||
import LightDarkImg from './LightDarkImg' | ||
|
||
const wrapper = (theme: Theme) => { | ||
return ({ children }: { children: React.ReactNode }) => { | ||
return ( | ||
<ThemeContext.Provider value={{ theme, setTheme: vi.fn() }}> | ||
{children} | ||
</ThemeContext.Provider> | ||
) | ||
} | ||
} | ||
|
||
describe('LightDarkImg', () => { | ||
const mockProps = { | ||
src: '/light-image.png', | ||
darkSrc: '/dark-image.png', | ||
alt: 'Test image', | ||
} | ||
|
||
it('renders with light source in light mode', () => { | ||
render(<LightDarkImg {...mockProps} />, { wrapper: wrapper(Theme.LIGHT) }) | ||
|
||
const img = screen.getByAltText('Test image') | ||
expect(img).toBeInTheDocument() | ||
expect(img).toHaveAttribute('src', '/light-image.png') | ||
}) | ||
|
||
it('renders with dark source in dark mode', () => { | ||
render(<LightDarkImg {...mockProps} />, { wrapper: wrapper(Theme.DARK) }) | ||
|
||
const img = screen.getByAltText('Test image') | ||
expect(img).toBeInTheDocument() | ||
expect(img).toHaveAttribute('src', '/dark-image.png') | ||
}) | ||
|
||
it('falls back to light source when dark source is not provided', () => { | ||
const propsWithoutDark = { | ||
src: '/light-image.png', | ||
alt: 'Test image', | ||
} | ||
|
||
render(<LightDarkImg {...propsWithoutDark} />, { | ||
wrapper: wrapper(Theme.DARK), | ||
}) | ||
|
||
const img = screen.getByAltText('Test image') | ||
expect(img).toBeInTheDocument() | ||
expect(img).toHaveAttribute('src', '/light-image.png') | ||
}) | ||
|
||
it('passes through additional props to img element', () => { | ||
render( | ||
<LightDarkImg {...mockProps} className="w-10" data-testid="test-img" />, | ||
{ wrapper: wrapper(Theme.LIGHT) } | ||
) | ||
|
||
const img = screen.getByAltText('Test image') | ||
expect(img).toHaveClass('w-10') | ||
expect(img).toHaveAttribute('data-testid', 'test-img') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
|
||
import { Theme, useThemeContext } from 'shared/ThemeContext' | ||
|
||
export interface LightDarkImgSrcProps { | ||
alt: string | ||
src: string | ||
darkSrc?: string | ||
} | ||
|
||
type LightDarkImgProps = Omit< | ||
React.ImgHTMLAttributes<HTMLImageElement>, | ||
'alt' | 'src' | ||
> & | ||
LightDarkImgSrcProps | ||
|
||
function LightDarkImg({ src, darkSrc, alt, ...props }: LightDarkImgProps) { | ||
const { theme } = useThemeContext() | ||
const isDarkMode = theme === Theme.DARK | ||
|
||
return ( | ||
<img {...props} alt={alt} src={isDarkMode && darkSrc ? darkSrc : src} /> | ||
) | ||
} | ||
|
||
export default LightDarkImg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './LightDarkImg' |