Hello,
On my website I successfully display images using Frontityās native component, Image
I need to pass to this component the width and height attributes explicitly, but it doesnāt render the attributes correctly.
As I see in the documentation it is possible to pass these attributes to the Iframe component.
Any idea how to do it
Johan
31 May 2021 16:31
2
You can simply pass them through the element just like a regular img tag:
<Image src="path/to/file" width="124" height="124" />
Easiest you can do is create a regular img element, and simply rename it to Image.
Thank you for your answer.
Unfortunately, your solution does not work for me.
In my code I have this.
<Image src="path/to/file" alt="My File" width="300" height="122" />
But then it renders this:
<img src="path/to/file" alt="My File" class="frontity-lazy-image" loading="lazy" style="" height="122" >
As you can see, the width attribute is lost, I donāt know the reason why it happensā¦
I have the latest versions of Frontity and its packages installed.
Thanks in advance
Hi @franfal1981 ,
Any width
prop passed to the <Image>
component should be passed to the final <img>
tag as you can see in the implementation of the <Image>
component
import * as React from "react";
import { connect, useConnect } from "frontity";
import { Frontity } from "frontity/types";
/**
* Props for the {@link Image} component.
*/
export interface ImageProps extends React.ImgHTMLAttributes<HTMLImageElement> {
/**
* CSS class(es).
*/
className?: string;
/**
* The `rootMargin` of useInView.
*
* @deprecated This property was used in the previous version of the Image
* component and has no effect anymore as the Image component relies only on
* native lazy-loading.
*/
This file has been truncated. show original
What I donāt see is the checking of this (passing width
prop) in the tests so maybe thereās a bug.
@David any thoughts on this? Should we add this width
prop use case to the tests of the <Image>
component?
Anyway @franfal1981 , can you please provide a repo or code-sandbox with your code ? This is especially helpful to find solutions to technical issues with specific code
Detailing the info suggested here when having issues will help the community to provide the best possible help as quickly and as efficiently as possible.
Hi there!
I have tried your suggestions again, and unfortunately it doesnāt workā¦
Only the height attribute is rendered, I sincerely believe that there is a bug in the component that prevents the width attribute to renderā¦
I imagine that this same thing will happen to more frontity usersā¦
I think it is something important to fix since google core web vitals recommends to specify in each image the 2 attributes (width and height).
Actually i have this code
<Image src={path-to-file} alt={alt-text} width={my-width} height="{my-height}"/>
And i get this
<img alt="alt text" class="frontity-lazy-image css-16mvcb1-Featured" loading="lazy" height="512" src="path-to-file">
As you can see width attribute is missingā¦
Regards!
Johan
2 June 2021 11:10
7
I can confirm that the width is not set as an attribute on master (@frontity /components: 1.7.3), but it does work on the dev branch.
/* eslint-disable jsx-a11y/alt-text */
import * as React from "react";
import { Head, connect } from "frontity";
import { Connect, Package } from "frontity/types";
import useInView from "@frontity/hooks/use-in-view";
// Hides any image rendered by this component that is not
// inside a <noscript> when JS is disabled.
const noJsStyles = `
:not(noscript) > .frontity-lazy-image {
display: none;
}
`;
// Finds all the images rendered by this component and maps
// their `data-src` and `data-srcset` attributes to `src` and `srcset`
// when the browser doesn't support Proxy or IntersectionObserver.
const noProxyScript = `
if (typeof window !== "undefined" && (!("Proxy" in window) || !("IntersectionObserver" in window))) {
This file has been truncated. show original
Thank you @Johan
@juanma do you think it is possible to add the width attribute in the next update of the package?
Regards
Johan
2 June 2021 15:55
9
The next update should have the updated Image component.
Although I could provide a hotfix if needed. Only adds 3 or 4 lines.
HI @Johan and @franfal1981
A fix for this issue has been implemented and it will available in the next release
frontity:master
ā frontity:changeset-release/master
opened 07:06AM - 03 Jun 21 UTC
This PR was opened by the [Changesets release](https://github.com/changesets/actā¦ ion) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to master, this PR will be updated.
# Releases
## @frontity/components@1.8.0
### Minor Changes
- [`2f988427`](https://github.com/frontity/frontity/commit/2f9884270490d1a67c3ba6f061831b24ae4cc038) [#826](https://github.com/frontity/frontity/pull/826) Thanks [@michalczaplinski](https://github.com/michalczaplinski)! - Make the Image component from `@frontity/components` AMP-aware and remove the Intersection Observer fallback which used to lazily load images in the absence of native lazy loading.
## @frontity/core@1.14.2
### Patch Changes
- [`a154a9cb`](https://github.com/frontity/frontity/commit/a154a9cb7093ab0199c01f28b6363f93427abb17) [#840](https://github.com/frontity/frontity/pull/840) Thanks [@luisherranz](https://github.com/luisherranz)! - Fix Webpack/TS-Node excludes not working on Windows.
- [`63154b4f`](https://github.com/frontity/frontity/commit/63154b4fb16095ccf22e4862c6d91c9c6157af67) [#837](https://github.com/frontity/frontity/pull/837) Thanks [@luisherranz](https://github.com/luisherranz)! - Fix transpilation of `frontity.config.js` files in external packages.