-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): responsive images with CPicture
closes #12
- Loading branch information
1 parent
ec527e1
commit 7a37dcb
Showing
15 changed files
with
265 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import CPicture from './CPicture'; | ||
|
||
describe('CPicture', () => { | ||
it('renders with the right attributes', () => { | ||
const wrapper = mount(CPicture, { | ||
props: { | ||
src: '/image.jpg', | ||
alt: 'alt', | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toBe( | ||
'<picture><img src="/image.jpg" alt="alt"></picture>' | ||
); | ||
}); | ||
|
||
it('renders an empty alt by default', () => { | ||
const wrapper = mount(CPicture, { | ||
props: { | ||
src: '/image.jpg', | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toBe( | ||
'<picture><img src="/image.jpg" alt=""></picture>' | ||
); | ||
}); | ||
|
||
it('apply config class on img tag', () => { | ||
const wrapper = mount(CPicture, { | ||
global: { | ||
provide: { | ||
$chusho: { | ||
options: { | ||
components: { | ||
picture: { | ||
class: 'picture', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
props: { | ||
src: '/image.jpg', | ||
class: 'special-picture', | ||
}, | ||
}); | ||
|
||
expect(wrapper.find('img').classes()).toEqual([ | ||
'special-picture', | ||
'picture', | ||
]); | ||
}); | ||
|
||
it('renders given sources', () => { | ||
const wrapper = mount(CPicture, { | ||
props: { | ||
src: '/image.jpg', | ||
sources: [ | ||
{ | ||
srcset: '[email protected] 2x, image.webp', | ||
type: 'image/webp', | ||
}, | ||
{ | ||
srcset: '[email protected] 2x, image.jpg', | ||
type: 'image/jpeg', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toBe( | ||
'<picture><source srcset="[email protected] 2x, image.webp" type="image/webp"><source srcset="[email protected] 2x, image.jpg" type="image/jpeg"><img src="/image.jpg" alt=""></picture>' | ||
); | ||
}); | ||
}); |
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,57 @@ | ||
import { | ||
defineComponent, | ||
h, | ||
inject, | ||
mergeProps, | ||
PropType, | ||
SourceHTMLAttributes, | ||
} from 'vue'; | ||
|
||
import { DollarChusho } from '../../types'; | ||
import { generateConfigClass } from '../../utils/components'; | ||
import componentMixin from '../mixins/componentMixin'; | ||
|
||
export default defineComponent({ | ||
name: 'CPicture', | ||
|
||
mixins: [componentMixin], | ||
|
||
inheritAttrs: false, | ||
|
||
props: { | ||
/** | ||
* Default/fallback image URL used in the `src` attribute. | ||
*/ | ||
src: { | ||
type: String, | ||
required: true, | ||
}, | ||
/** | ||
* Alternative text description; leave empty if the image is not a key part of the content, otherwise describe what can be seen. | ||
*/ | ||
alt: { | ||
type: String, | ||
default: '', | ||
}, | ||
/** | ||
* Generate multiple `source` elements with the given attributes. | ||
*/ | ||
sources: { | ||
type: Array as PropType<SourceHTMLAttributes[]>, | ||
default: () => [], | ||
}, | ||
}, | ||
|
||
render() { | ||
const pictureConfig = inject<DollarChusho | null>('$chusho', null)?.options | ||
?.components?.picture; | ||
const elementProps: Record<string, unknown> = mergeProps(this.$attrs, { | ||
src: this.$props.src, | ||
alt: this.$props.alt, | ||
...generateConfigClass(pictureConfig?.class, this.$props), | ||
}); | ||
const sources = this.$props.sources.map((source) => h('source', source)); | ||
|
||
return h('picture', null, [...sources, h('img', elementProps)]); | ||
}, | ||
}); |
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,3 @@ | ||
import CPicture from './CPicture'; | ||
|
||
export { CPicture }; |
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,56 @@ | ||
# Picture | ||
|
||
Easily generate responsive images. | ||
|
||
## Config | ||
|
||
### class | ||
|
||
Classes applied to the component `img` element, except when the prop `bare` is set to `true`. See [styling components](/guide/styling-components/). | ||
|
||
- **type:** `Array<String | Object> | Object | String | (props: Object) => {}` | ||
- **default:** `null` | ||
|
||
#### Example | ||
|
||
```js | ||
class: 'img-responsive' | ||
``` | ||
|
||
## API | ||
|
||
<Docgen :components="['CPicture']" /> | ||
|
||
## Examples | ||
|
||
### Simplest | ||
|
||
```vue | ||
<CPicture src="/path/to/image.jpg" /> | ||
``` | ||
|
||
### With sources | ||
|
||
```vue | ||
<CPicture | ||
:src="src" | ||
:sources="[ | ||
{ | ||
srcset: '/path/to/[email protected] 2x, /path/to/image.webp', | ||
type: 'image/webp', | ||
}, | ||
{ | ||
srcset: '/path/to/[email protected] 2x, /path/to/image.jpg', | ||
type: 'image/jpeg', | ||
}, | ||
]" | ||
/> | ||
``` | ||
|
||
### With additional attributes | ||
|
||
Attributes are not applied to the `picture` element but to the `img` element. | ||
|
||
```vue | ||
<CPicture src="/path/to/image.jpg" alt="Description" loading="lazy" /> | ||
``` |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
15 changes: 15 additions & 0 deletions
15
packages/playground/src/components/examples/components/picture/Default.vue
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,15 @@ | ||
<template> | ||
<CPicture :src="src" class="img-responsive" /> | ||
</template> | ||
|
||
<script> | ||
import jpg from '../../../../assets/images/building.jpg'; | ||
export default { | ||
data() { | ||
return { | ||
src: jpg, | ||
}; | ||
}, | ||
}; | ||
</script> |
35 changes: 35 additions & 0 deletions
35
packages/playground/src/components/examples/components/picture/WithSources.vue
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,35 @@ | ||
<template> | ||
<CPicture | ||
:sources="sources" | ||
:src="src" | ||
alt="Sky view from the middle of a C shaped building." | ||
width="825" | ||
height="550" | ||
loading="lazy" | ||
/> | ||
</template> | ||
|
||
<script> | ||
import jpg from '../../../../assets/images/building.jpg'; | ||
import webp from '../../../../assets/images/building.webp'; | ||
import jpg2x from '../../../../assets/images/[email protected]'; | ||
import webp2x from '../../../../assets/images/[email protected]'; | ||
export default { | ||
data() { | ||
return { | ||
src: jpg, | ||
sources: [ | ||
{ | ||
srcset: `${webp2x} 2x, ${webp}`, | ||
type: 'image/webp', | ||
}, | ||
{ | ||
srcset: `${jpg2x} 2x, ${jpg}`, | ||
type: 'image/jpeg', | ||
}, | ||
], | ||
}; | ||
}, | ||
}; | ||
</script> |
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