Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<Avatar/> supports svgs passed to image prop #1040

Merged
merged 36 commits into from
Feb 7, 2023

Conversation

r100-stack
Copy link
Member

@r100-stack r100-stack commented Jan 31, 2023

Changes

Avatar's image prop can now accept <svg> children and display it correctly.

Testing

Added a unit test to confirm svgs passed to the image prop is included in the dom. Added a With Icon story and tested the the svg displays properly with the background color.

Docs

Added a changeset

TODOs

  • Add/approve visual and unit tests

@r100-stack r100-stack self-assigned this Jan 31, 2023
@r100-stack r100-stack linked an issue Jan 31, 2023 that may be closed by this pull request
@r100-stack r100-stack marked this pull request as ready for review January 31, 2023 21:41
@r100-stack r100-stack requested a review from a team as a code owner January 31, 2023 21:41
@r100-stack r100-stack requested review from a team, FlyersPh9 and adamhock and removed request for a team January 31, 2023 21:41
@mayank99
Copy link
Contributor

mayank99 commented Jan 31, 2023

Maybe I missed something, but what is the benefit of using <Icon> inside <Avatar> if the size is being overridden anyway? The user should just pass an svg directly.

Also, what is the benefit of the deprecation? The same image prop could be reused.

The explicit iui-icon class also does not need to be supported.

@r100-stack
Copy link
Member Author

Maybe I missed something, but what is the benefit of using <Icon> inside <Avatar> if the size is being overridden anyway? The user should just pass an svg directly.

There's no benefit. I just thought that since we're allowing users to wrap svgs in <Icon>, they might do this all the time. So they might also pass <Icon><SvgAdd/></Icon> to Avatar. So we should catch this case also.

Also, what is the benefit of the deprecation? The same image prop could be reused.

I could reuse the image prop. I thought of changing it to child to make it more clear that svgs (or other elements) can be passed.

The explicit iui-icon class also does not need to be supported.

I included that test since that is how icons are added in avatars in the avatar HTML demo page.

image

Also, the styling in avatar.scss targets .iui-icon (https://github.com/iTwin/iTwinUI/pull/1040/files#diff-be3c849b1fcfeb4dda01cfe9c31aa0e237ab0e06d71a2838d5ddd77c1700f05aL51-R53)

@mayank99
Copy link
Contributor

There's no benefit. I just thought that since we're allowing users to wrap svgs in <Icon>, they might do this all the time. So they might also pass <Icon><SvgAdd/></Icon> to Avatar. So we should catch this case also.

If there is no benefit, then lets not add the extra complexity. Currently the icon component is coupled to the avatar for no reason.

I could reuse the image prop. I thought of changing it to child to make it more clear that svgs (or other elements) can be passed.

image seems fine to me. svgs are technically still images. But if you want to deprecate it, then you could use children instead of child. it feels more natural in JSX.

The explicit iui-icon class also does not need to be supported.

I included that test since that is how icons are added in avatars in the avatar HTML demo page.

Also, the styling in avatar.scss targets .iui-icon (#1040 (files))

The class names are considered "implementation detail" for React components. It's totally fine to support them in CSS, but React users are never expected to pass the iui-icon class. In the future we could rename this class to iui-avatar-icon or something, and that should not break react users.

packages/itwinui-css/src/avatar/avatar.scss Outdated Show resolved Hide resolved
packages/itwinui-react/src/core/Avatar/Avatar.tsx Outdated Show resolved Hide resolved
apps/storybook/src/Avatar.stories.tsx Outdated Show resolved Hide resolved
@r100-stack r100-stack changed the title Avatar deprecate image prop in favor of child prop <Avatar/> supports svgs passed to image prop Jan 31, 2023
@r100-stack r100-stack added enhancement New feature or request react Needs change in react package labels Jan 31, 2023
@@ -48,7 +48,7 @@
font-size: var(--_iui-avatar-font-size);
}

> .iui-icon {
svg {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this considered a breaking change for our CSS pkg?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ohh, right, it looks like a breaking change. It also breaks the avatar demo html page.

image

I auto-committed the suggestion (#1040 (comment)), but I think it needs some editing. Will look into it more.

Copy link
Member Author

@r100-stack r100-stack Jan 31, 2023

Choose a reason for hiding this comment

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

Would this be alright? This keeps the html demos the same, is not a breaking change, and also targets svgs without .iui-icon which is likely how icons are passed on the react side.

Suggested change
svg {
> .iui-icon, > svg {

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like you need to add these three lines to fix the positioning

left: 50%;
top: 50%;
transform: translate(-50%, -50%);

and then the wrapping section would need display: flex; align-items: end

alternatively, display: grid could be used instead of position: absolute to center the svg. I think this would avoid the need to set styles on the wrapper.

i do not think it would be a breaking change because anyone using iui-icon will be unaffected.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, the first option you mentioned worked. Now targeting the avatar's svg instead of > .iui-icon (7a2f509 (#1040))

Copy link
Contributor

@mayank99 mayank99 left a comment

Choose a reason for hiding this comment

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

Looks good. Just need @FlyersPh9's help to find a new default color (#1040 (comment))

Copy link
Collaborator

@FlyersPh9 FlyersPh9 left a comment

Choose a reason for hiding this comment

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

Good to go once this change is made.

@mayank99 mayank99 merged commit 054bc3b into main Feb 7, 2023
@mayank99 mayank99 deleted the rohan/avatar-react-pass-svg branch February 7, 2023 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request react Needs change in react package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avatar React component should support passing svg children
3 participants