Skip to content

Commit

Permalink
news picture prefers-color-scheme support
Browse files Browse the repository at this point in the history
  • Loading branch information
samipe committed Apr 25, 2024
1 parent 85b6e7a commit 8c6b701
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/components/News.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
:key="entry.id"
class="flex border-bottom-bg p-medium">
<div class="col-sm-3 pr-xsmall">
<img v-if="entry.image" :src="`${entry.image.file.url}${imgUrlParams}`" class="main block" />
<picture v-if="entry.image?.file?.url" class="main block">
<source :srcset="`${entry.image?.file?.url}${imgUrlParams}`" media="(prefers-color-scheme: light)"/>
<source v-if="entry.imageDark" :srcset="`${entry.imageDark.file?.url}${imgUrlParams}`" media="(prefers-color-scheme: dark)"/>
<!-- fallback -->
<img :src="`${entry.image?.file?.url}${imgUrlParams}`"/>
</picture>
<robot-icon v-else size="1.75rem" style="float: right"/>
</div>
<div class="col-sm-9 pl-xsmall">
<a v-if="entry.link" :href="entry.link" target="blank" class="mb-none line-height-1">
Expand All @@ -42,7 +48,11 @@
<script>
import { getNews } from '../js/contentfulClient'
import { documentToHtmlString } from '@contentful/rich-text-html-renderer'
import { RobotIcon } from './icons'
export default {
components: {
RobotIcon
},
data: () => ({
expanded: false,
news: [],
Expand Down Expand Up @@ -103,10 +113,10 @@ export default {
}
@media (prefers-color-scheme: dark) {
.inner-news-container:not(.expanded)::after {
background: linear-gradient(transparent 80%, #1a1a1a 100%);
background: linear-gradient(transparent 80%, #1a1a1a 100%);
}
}
img.main {
picture.main, picture img {
width: 100%;
border-radius: 0.25rem;
}
Expand Down
3 changes: 2 additions & 1 deletion src/js/contentfulClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const getNews = async() => {
})
.then(({ items }) => items.map(({ fields }) => ({
...fields,
image: fields.image?.fields
image: fields.image?.fields,
imageDark: fields.imageDark?.fields // optional, for darkmode, eg white text variant
})))
return items
}
Expand Down

0 comments on commit 8c6b701

Please sign in to comment.