Skip to content

feat(tools): add image component - #44590

Merged
ahmaxed merged 1 commit into
freeCodeCamp:mainfrom
Ismailtlem:feat/add-image-component
Feb 3, 2022
Merged

ahmaxed merged 1 commit into
freeCodeCamp:mainfrom
Ismailtlem:feat/add-image-component

Conversation

@Ismailtlem

Copy link
Copy Markdown
Member

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the main branch of freeCodeCamp.
  • I have tested these changes either locally on my machine, or GitPod.

Related to #41580

@Ismailtlem
Ismailtlem requested a review from a team December 26, 2021 11:22
@gitpod-io

gitpod-io Bot commented Dec 26, 2021

Copy link
Copy Markdown

@Ismailtlem
Ismailtlem marked this pull request as draft December 26, 2021 11:22
@github-actions github-actions Bot added the scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. label Dec 26, 2021
@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch 2 times, most recently from 7afc742 to c84ede3 Compare December 26, 2021 12:43
@Ismailtlem
Ismailtlem marked this pull request as ready for review December 26, 2021 13:39
@gikf gikf added the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label Dec 30, 2021
Comment thread tools/ui-components/src/image/image.css Outdated
.img-responsive {
display: block;
max-width: 100%;
height: auto;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the tailwind classes instead of writing custom CSS

@ahmaxed ahmaxed self-assigned this Jan 3, 2022
@ahmaxed

ahmaxed commented Jan 3, 2022

Copy link
Copy Markdown
Member

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.

Comment thread tools/ui-components/src/image/Image.tsx Outdated
Comment on lines +5 to +7
alt?: string;
className?: string;
src?: string;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alt and source should not be optional.

Comment thread tools/ui-components/src/image/Image.tsx Outdated
Comment on lines +10 to +12
export const Image = ({ alt, className, src }: ImageProps): JSX.Element => {
return <img alt={alt} src={src} className={className} />;
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encourage you to take a look at the react-boostrap component for the struecutre and return a React.forwardRef

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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({});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more descriptive name should be used. If this is the default version, it can be named default.

};

export const Second = Template.bind({});
Second.args = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be named avatar.

@ahmaxed ahmaxed mentioned this pull request Jan 4, 2022
37 of 49 tasks
@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch from c84ede3 to f83cc4c Compare January 4, 2022 21:32
@ghost

ghost commented Jan 4, 2022 •

Copy link
Copy Markdown

👀 Review this PR in a CodeSee Review Map

Preview the Review Map

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch 2 times, most recently from 09be8e1 to d91b8af Compare January 9, 2022 21:33
@ahmaxed

ahmaxed commented Jan 14, 2022

Copy link
Copy Markdown
Member

Thanks for working on this feature.
It is coming together pretty great.
Let me know when it is done so I could take an in-depth look.

@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch from d91b8af to 9ae7612 Compare January 16, 2022 15:25
@Ismailtlem

Ismailtlem commented Jan 16, 2022 •

Copy link
Copy Markdown
Member Author

I don't think I will add anything (unless I missed something)
Please have a look @ahmadabdolsaheb whenever you have some available time

@huyenltnguyen huyenltnguyen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ismailtlem Thanks a lot for the PR, and apologize for the delay in reviewing it.

Comment on lines +15 to +19
const baseArgs = {
alt: '',
src: '',
ref: ref
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

Comment on lines +1 to +2
/* eslint-disable react/prop-types */
/* eslint-disable react/display-name */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, if it's decorative, we want alt="", rather than omitting it. This is what w3 has to say.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, yeah defaulting to an empty string sounds good!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sorry, now that I re-read my comment, I wasn't being clear at all. Thanks for making me clarify it 😄

@huyenltnguyen huyenltnguyen added status: waiting update Is awaiting update, after feedback or request for changes and removed status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. labels Jan 26, 2022
@ahmaxed

ahmaxed commented Jan 27, 2022 •

Copy link
Copy Markdown
Member

One last note, if we need to style this component, please make sure the styles are added as tailwind classes to the image element.

@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch from 2a2e9b9 to 2e613a3 Compare January 27, 2022 20:14
@Ismailtlem Ismailtlem closed this Jan 27, 2022
@Ismailtlem Ismailtlem reopened this Jan 27, 2022
@Ismailtlem

Copy link
Copy Markdown
Member Author

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 ?

@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch 2 times, most recently from f6a8ee7 to 587c24c Compare January 29, 2022 13:14
@Ismailtlem
Ismailtlem force-pushed the feat/add-image-component branch from 587c24c to 30b86b5 Compare January 29, 2022 13:54

@huyenltnguyen huyenltnguyen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

.certification-namespace .signatures img {
max-width: 300px;
width: 100%;
margin: 0 auto;
}
😅

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.

Comment on lines +23 to +29
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'
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@huyenltnguyen huyenltnguyen added status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. and removed status: waiting update Is awaiting update, after feedback or request for changes labels Jan 30, 2022
@ahmaxed

ahmaxed commented Jan 31, 2022

Copy link
Copy Markdown
Member

@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:

img {
  border: 0;
}
img {
  vertical-align: middle;
}
img {
  page-break-inside: avoid;
}
img {
  max-width: 100% !important;
}

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?

@huyenltnguyen

Copy link
Copy Markdown
Member

Oh thanks for pointing that out, Ahmad!

Tailwind offers preflight styles, which are included in @tailwind base, and we already have it in ui-components.

The following rules are set to img in the preflight stylesheet:

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 border: 0 is still needed. Looking at the source code of normalize.css, they need this rule to ensure images are rendered properly in IE10, but I don't believe we need to support this browser. (Normalize sets border-style to none rather than border to 0, but if I understand the spec correctly, they essentially have the same effect, as border style is none by default and therefore the border is not painted, regardless of its width.)

As for page-break-inside, it is deprecated in favor of break-inside, so we will need to use that property instead.

@ahmaxed

ahmaxed commented Feb 3, 2022 •

Copy link
Copy Markdown
Member

I don't see break-inside in the newer bootstrap, so we can get this in for now. Thank you @Ismailtlem, @huyenltnguyen

@ahmaxed
ahmaxed merged commit b0db8ec into freeCodeCamp:main Feb 3, 2022
@huyenltnguyen

Copy link
Copy Markdown
Member

Woot woot! Another component is in! 🎉 Thanks a lot @Ismailtlem!

@Ismailtlem
Ismailtlem deleted the feat/add-image-component branch February 6, 2022 12:17
@Sboonny Sboonny mentioned this pull request Jul 14, 2022
2 of 35 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants