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

Consistent spacing between Icon/Text #130

Open
meissadia opened this issue Aug 8, 2023 · 13 comments · May be fixed by #131
Open

Consistent spacing between Icon/Text #130

meissadia opened this issue Aug 8, 2023 · 13 comments · May be fixed by #131
Assignees
Labels
enhancement New feature or request FEWD

Comments

@meissadia
Copy link
Contributor

meissadia commented Aug 8, 2023

  1. I'm imagining an entry in Storybook along the lines of our Icon tables that would display the following scenarios for easy comparison.

    • Text with square icon
    • Text with round icon
    • Text with plain icon
    • Text with icon on the left
    • Text with icon on the right
  2. And similar to the way jump icons are handled, we would want to enforce consistent spacing between icon + text via CSS, so this will need to be implemented.

@meissadia meissadia self-assigned this Aug 8, 2023
@meissadia meissadia linked a pull request Aug 8, 2023 that will close this issue
@meissadia
Copy link
Contributor Author

meissadia commented Aug 8, 2023

I've calculated the width of a   in Avenir Next by

  • Measuring the width of the text text space, which includes a space (107.750px)
  • Measure the width of the text textspace, without the space (102.793px)
  • Calculating the difference, then dividing by the font-size (16px) to get the size in em
    • (107.750 - 102.793) / 16 = 0.3098125em

I've then used that as the base margin between icon and text for our prototype.

https://deploy-preview-131--cfpb-design-stories.netlify.app/?path=/story/components-icon--icon-with-text

@natalia-fitzgerald
Copy link

natalia-fitzgerald commented Aug 9, 2023

@meissadia

In looking at the table:

  • Known use cases include: Icon + Text / Text + Icon / Round Icon + Text
  • We can remove the square icon examples. The square icons are for social media and typically appear all together in a row and without text.
  • I don’t think there’s a use case for a round icon to the right of text (@contolini can you think of any?)
  • Is there something going on with the vertical alignment of the icons in the table. The icons look misaligned vertically (they look high).

@meissadia
Copy link
Contributor Author

@natalia-fitzgerald Updated to reflect known use cases

Icon alignment looks improved in the latest deployment. I didn't change anything to specifically address that...so not sure what was going wrong in the first iteration.

Alignment still looks off for <p> and <a> elements? But I'm not sure why.

@meissadia meissadia added the enhancement New feature or request label Aug 17, 2023
@meissadia meissadia changed the title [Story] Icon with text Consistent spacing between Icon/Text Aug 17, 2023
@meissadia
Copy link
Contributor Author

meissadia commented Sep 5, 2023

Alignment still looks off for <p> and <a> elements? But I'm not sure why.

@natalia-fitzgerald Issue seems related to a couple things:

  • Icon sitting next to a lower case character
  • Lower case character having an ascender vs descender
    • icon looks well aligned next to letter with ascender (h)
    • looks off next to a descender (p) or neither (a)
Lower case / descender Upper case Ascender
Screenshot 2023-09-05 at 5 00 11 PM Screenshot 2023-09-05 at 5 00 42 PM Screenshot 2023-09-05 at 5 05 15 PM

Not sure there is a consistent styling fix for this or if it would fall under design usage guidelines i.e. Don't use icons next to words that begin with a lower case letter.

@natalia-fitzgerald
Copy link

@meissadia - Can you show this table with a circle icon for comparison?

@meissadia
Copy link
Contributor Author

@natalia-fitzgerald

Lower case / descender Upper case Ascender
Screenshot 2023-09-06 at 2 31 15 PM Screenshot 2023-09-06 at 2 31 59 PM Screenshot 2023-09-06 at 2 34 15 PM

@natalia-fitzgerald
Copy link

@meissadia - Does the vertical alignment shown here match what is done on the rest of the site and in the Design System?

@meissadia
Copy link
Contributor Author

@natalia-fitzgerald I hacked together this example by editing the HTML on the CFPB Iconography page and, yes, it appears that this behavior is consistent with the CFPB DS, and thus should also reflect how the SBL site would render icons.

Screenshot 2023-09-06 at 3 06 46 PM

@meissadia
Copy link
Contributor Author

Trying to find a way to accomplish this consistent spacing via CSS, so that devs would not need to encapsulate text+icon in any special wrappers...but it seems that CSS does not have the ability to recognize plain text. So targeting scenarios where the icon comes before/after text, and applying the appropriate spacing, is proving more involved than I'd hoped.

After listing out the investigation below, I'm tempted to say that continuing with the current approach of "Devs just need to insert a &nbsp; between an icon and adjacent text" may actually be the most sustainable approach.

Alternative approaches:

  • Add props to the <Icon /> component that accept text and handle the spacing
    • Ex: <Icon name='info' textRight="This text would display to the right of the icon with a margin between" />
    • Ex: <Icon name='info' textLeft="This text would display to the left of the icon with a margin between" />
    • Pros
      • Makes it easy to create both the icon and it's accompanying text in a single component
    • Cons
      • May not be the most intuitive approach so we would want to call this out in our documentation
  • Create an helper/wrapper component for text that appears next to an icon, which we could then target via CSS
    • Ex. <><Icon name='info' /><IconText>This text appears next to an icon</IconText><>
    • Pros
      • Takes care of the need for devs to be responsible for inserting the space between icon/text
    • Cons
      • Devs still have to know to wrap icon-adjacent text in the special wrapper in order to get the proper spacing
  • Integrate an icon prop into components that commonly display icons
    • Ex. Link w/ icon: <Link href="#" icon="download">Download font</Link>
      • This would automatically insert the named icon after the link text with appropriate styling and spacing.

Examples from CF.gov of text + icon

@meissadia
Copy link
Contributor Author

DS Heading with Icon variant: https://cfpb.github.io/design-system/foundation/headings#variations-1

@meissadia
Copy link
Contributor Author

Classes used in the examples above

  • Owning a home (meta header): .m-meta-header_item .cf-icon-svg
  • Activity log (heading icon): .a-heading__icon
  • Owning a home (plain icon): .cf-icon-svg

Possible solution:

  • Apply left and right margin to all .cf-icon-svg
    • If icon is first element within it's container, remove the left margin
    • If icon is last element within it's container, remove the right margin
    • Otherwise, leave margins as-is to provide consistent space between icons and everything else
    • Side effects
      • Would need to adjust .a-heading__icon styles

TODO: I need to try this out in code to see how achievable it is.

@meissadia
Copy link
Contributor Author

@natalia-fitzgerald

So I don't think we can achieve the consistent spacing we're looking for without requiring significant retrofitting for all existing usages of DS icons.

The table below demonstrates an extreme example of the approach described above.

In the first two columns the SVG element appears next to raw text i.e. <svg>Text
In the last two columns, the text is wrapped in an HTML element i.e. <svg><span>Text</span>

CSS cannot target raw text, so in order to apply CSS based spacing between icons and text, we'd need to wrap that text in an HTML element (last two columns), which means this approach would need to be retroactively applied in ALL instances where the spacing is currently achieved by putting a space(' ' or  ) between the icon and the text.

Screenshot 2023-10-06 at 1 04 20 PM

This seems like a significant overhaul to all of CF.gov and probably outside the scope of our current efforts.

That said, we can still achieve this consistent spacing in DSR and the SBL app. So we can discuss if we'd like to move forward with this.

@natalia-fitzgerald
Copy link

@meissadia
Nice work looking into this! We should share it with the DS team for awareness even if it’s not feasible on the larger scale. Next week let’s discuss what it would mean to implement this within the DSR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request FEWD
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants