Skip to content

docs.as-html: move tooltips to hidden spot in dom#2878

Merged
pchiusano merged 1 commit intotrunkfrom
allow-rich-tooltips
Feb 9, 2022
Merged

docs.as-html: move tooltips to hidden spot in dom#2878
pchiusano merged 1 commit intotrunkfrom
allow-rich-tooltips

Conversation

@hojberg
Copy link
Member

@hojberg hojberg commented Feb 9, 2022

Problem

Rich tooltips use block elements, which, in the html spec, and in how browsers interpret it, are not allowed inside

elements. This results in the browser closing the

as soon as it encounters a block element, effectively moving that block element outside of the narrative flow.

Solution

To fix this, when rendering docs as html, collect the contents of tooltips in a separate collection (using State and Writer) from the rest of the document, and insert them at the end of the DOM in a hidden div. Each tooltip trigger has a data-tooltip-content-id attribute that can be used to render the tooltip in-place when hovering with additional JavaScript.

Notes

Paired with @pchiusano

@hojberg hojberg requested review from pchiusano and rlmark February 9, 2022 15:22
Copy link
Member Author

Choose a reason for hiding this comment

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

There's a little bit of formatting noise due to this change.

Rich tooltips use block elements, which, in the html spec, and in how
browsers interpret it, are not allowed inside <p> elements. This results
in the browser closing the <p> as soon as it encounters a block element,
effectively moving that block element outside of the narrative flow.

To fix this, when rendering docs as html, collect the contents of
tooltips in a separate collection (using `State` and `Writer`) from the
rest of the document, and insert them at the end of the dom in a hidden
div. Each tooltip trigger has a `data-tooltip-content-id` attribute that
can be used to render the tooltip in-place when hovering with additional
JavaScript.
@hojberg hojberg force-pushed the allow-rich-tooltips branch from 935bc6a to cc4c641 Compare February 9, 2022 15:23
article_ [class_ "unison-doc"] $ do
content
div_ [class_ "tooltips", style_ "display: none;"] $ sequence_ tooltips
where
Copy link
Member Author

Choose a reason for hiding this comment

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

Changed this from a let..in to.a where.

toHtml_ sectionLevel
article_ [class_ "unison-doc"] $ do
content
div_ [class_ "tooltips", style_ "display: none;"] $ sequence_ tooltips
Copy link
Member Author

Choose a reason for hiding this comment

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

This is where the tooltips are added at the end of the DOM in a hidden div.

[ class_ "tooltip-trigger",
data_ "tooltip-content-id" tooltipId
]
<$> currentSectionLevelToHtml triggerContent
Copy link
Member Author

Choose a reason for hiding this comment

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

Each tooltip is given an id (a number incremented through State), which is added as an html id to the content and as a data attribute to tooltip triggers to tie the pair together.

data_ "tooltip-content-id" tooltipId
]
<$> currentSectionLevelToHtml triggerContent
Word word ->
Copy link
Member Author

Choose a reason for hiding this comment

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

Changes below here are to satisfy the return type with State and Writer.

Copy link
Member

@pchiusano pchiusano left a comment

Choose a reason for hiding this comment

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

This came out pretty nice!

I wondered later if you could directly work with HtmlT (WriterT . StateT); it might come out even nicer. But I'd leave it alone.

@pchiusano pchiusano merged commit 84675af into trunk Feb 9, 2022
@pchiusano pchiusano deleted the allow-rich-tooltips branch February 9, 2022 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants