Repository navigation
feat(tools): add image component - #44590
Conversation
7afc742 to
c84ede3
Compare
| .img-responsive { | ||
| display: block; | ||
| max-width: 100%; | ||
| height: auto; |
There was a problem hiding this comment.
You should use the tailwind classes instead of writing custom CSS
|
Please take a look at the #43835 for a reference on the research and development. I will recreate the issue and add related info accordingly shortly. |
| alt?: string; | ||
| className?: string; | ||
| src?: string; |
There was a problem hiding this comment.
Alt and source should not be optional.
| export const Image = ({ alt, className, src }: ImageProps): JSX.Element => { | ||
| return <img alt={alt} src={src} className={className} />; | ||
| }; |
There was a problem hiding this comment.
I encourage you to take a look at the react-boostrap component for the struecutre and return a React.forwardRef
There was a problem hiding this comment.
Hi @ahmadabdolsaheb
Can I please know what will be the usage of ref in this context ? I am not sure I am getting it (beside the fact that this is how the Image component is defined in react-bootstrap, unless this is the only reason)
| src: '' | ||
| }; | ||
|
|
||
| export const Simple = Template.bind({}); |
There was a problem hiding this comment.
more descriptive name should be used. If this is the default version, it can be named default.
| }; | ||
|
|
||
| export const Second = Template.bind({}); | ||
| Second.args = { |
c84ede3 to
f83cc4c
Compare
|
👀 Review this PR in a CodeSee Review Map |
09be8e1 to
d91b8af
Compare
|
Thanks for working on this feature. |
d91b8af to
9ae7612
Compare
|
I don't think I will add anything (unless I missed something) |
huyenltnguyen
left a comment
There was a problem hiding this comment.
@Ismailtlem Thanks a lot for the PR, and apologize for the delay in reviewing it.
| const baseArgs = { | ||
| alt: '', | ||
| src: '', | ||
| ref: ref | ||
| }; |
There was a problem hiding this comment.
We can move these base args to Default.args, then reuse Default.args instead. No problem with the current approach, but I think having a single default/base set of arguments would help reduce the maintenance effort a bit 🙂
And since alt is a required prop, we should give it a valid string.
export const Default = Template.bind({});
Default.args = {
alt: 'Quincy Larson's signature',
src: 'https://cdn.freecodecamp.org/platform/english/images/quincy-larson-signature.svg',
ref
};
export const Avatar = Template.bind({});
Avatar.args = {
...Default.args,
// prop overrides
}| /* eslint-disable react/prop-types */ | ||
| /* eslint-disable react/display-name */ |
There was a problem hiding this comment.
I think we will encounter these a lot, so we might want to update the eslint config to disable these rules for the component library. I can help you with this in a follow-up PR, so don't worry.
| @@ -0,0 +1,5 @@ | |||
| export interface ImageProps { | |||
There was a problem hiding this comment.
Nit: Since this file does not contain components, we can just use the .ts extension instead.
| @@ -0,0 +1,5 @@ | |||
| export interface ImageProps { | |||
| alt: string; | |||
There was a problem hiding this comment.
Technically we can omit alt if the image is purely for decoration but let's keep it required for now to promote the accessibility practice. We can revisit this later.
There was a problem hiding this comment.
I think, if it's decorative, we want alt="", rather than omitting it. This is what w3 has to say.
There was a problem hiding this comment.
Ah I was thinking about making this prop optional and default to an empty string if it is not defined. I guess it would be a bit more convenient for the components that use this one. On the other hand, we can easily have our images inaccessible if we forget to pass alt to the component.
There was a problem hiding this comment.
Oh, sorry, yeah defaulting to an empty string sounds good!
There was a problem hiding this comment.
No, sorry, now that I re-read my comment, I wasn't being clear at all. Thanks for making me clarify it 😄
|
One last note, if we need to style this component, please make sure the styles are added as tailwind classes to the image element. |
2a2e9b9 to
2e613a3
Compare
|
Hi @ahmadabdolsaheb Do we want to apply the tailwind classes to every Image story? The Image component used here https://github.com/freeCodeCamp/freeCodeCamp/blob/main/client/src/client-only-routes/show-certification.tsx#L359 isn't using the responsive attribute, no ? |
f6a8ee7 to
587c24c
Compare
587c24c to
30b86b5
Compare
huyenltnguyen
left a comment
There was a problem hiding this comment.
The Image component used here main/client/src/client-only-routes/show-certification.tsx#L359 isn't using the responsive attribute
The styles aren't applied to the Image component directly, but through CSS selector
freeCodeCamp/client/src/pages/certification.css
Lines 143 to 147 in 73e5c4d
At the current stage, I don't know if there can be any CSS rules applied to our Image component by default. I guess we could just let the consumers pass in class names to style it, and if later on we realize that some CSS rules are being repetitive we can always come back and add them to the component.
As for the className prop, I was looking at the tailwindcss-classnames library as I wanted to see if we could improve the typing or prevent typos. (I work at a place where the design system does not allow the className prop, so seeing className allowing any arbitrary string makes me a bit nervous). Let me research on this a little more, but the current component interface looks good to me.
| export const Avatar = Template.bind({}); | ||
| Avatar.args = { | ||
| ref: ref, | ||
| alt: "camper's avatar", | ||
| src: 'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png', | ||
| className: 'object-cover img-fluid max-w-full h-auto' | ||
| }; |
There was a problem hiding this comment.
I forgot to add a note here. If we ended up using Image for a lot of avatars, we could have a standalone Avatar component as well.
|
@Ismailtlem, so bootstrap is adding some classes to elements by default. Some of those are coming from normalize that we will include in our css when bundling. We initially want to include the CSS styles that are in bootstrap, but do not appear in normalize. Here are the styles used for the image element in bootstrap 3.3.7: The older versions of normalize include the first style. For the rest, we should probably add them as tailwind styles to the image component. @huyenltnguyen thought? |
|
Oh thanks for pointing that out, Ahmad! Tailwind offers preflight styles, which are included in The following rules are set to display: block;
vertical-align: middle;
max-width: 100%;
height: auto;And that leaves us with border: 0;
page-break-inside: avoid;I'm not sure if As for |
|
I don't see break-inside in the newer bootstrap, so we can get this in for now. Thank you @Ismailtlem, @huyenltnguyen |
|
Woot woot! Another component is in! 🎉 Thanks a lot @Ismailtlem! |

Checklist:
Update index.md)mainbranch of freeCodeCamp.Related to #41580