From 6dcdad028e94f7e9fc3d5fbb5a315c12f726ab7e Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 20 Jun 2019 12:03:22 +0100 Subject: [PATCH 1/4] Clone ContextualMenu to InteractiveTooltip As part of reactions and editing work, we're adding a new style of tooltip that allows interacting with the content of the tooltip. `ContextualMenu` is closest out of the things we have today, but it doesn't position in quite the way we want and it's already quite complex. To get started, let's first clone that to a new `InteractiveTooltip`. Part of https://github.com/vector-im/riot-web/issues/9753 Part of https://github.com/vector-im/riot-web/issues/9716 --- res/css/_components.scss | 1 + .../views/elements/_InteractiveTooltip.scss | 164 ++++++++++++++++++ .../views/elements/InteractiveTooltip.js | 141 +++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 res/css/views/elements/_InteractiveTooltip.scss create mode 100644 src/components/views/elements/InteractiveTooltip.js diff --git a/res/css/_components.scss b/res/css/_components.scss index 582dc59517e..fa388c4e6ac 100644 --- a/res/css/_components.scss +++ b/res/css/_components.scss @@ -86,6 +86,7 @@ @import "./views/elements/_Field.scss"; @import "./views/elements/_ImageView.scss"; @import "./views/elements/_InlineSpinner.scss"; +@import "./views/elements/_InteractiveTooltip.scss"; @import "./views/elements/_ManageIntegsButton.scss"; @import "./views/elements/_MemberEventListSummary.scss"; @import "./views/elements/_MessageEditor.scss"; diff --git a/res/css/views/elements/_InteractiveTooltip.scss b/res/css/views/elements/_InteractiveTooltip.scss new file mode 100644 index 00000000000..11f548fa180 --- /dev/null +++ b/res/css/views/elements/_InteractiveTooltip.scss @@ -0,0 +1,164 @@ +/* +Copyright 2019 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +.mx_InteractiveTooltip_wrapper { + position: fixed; + z-index: 5000; +} + +.mx_InteractiveTooltip_background { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + opacity: 1.0; + z-index: 5000; +} + +.mx_InteractiveTooltip { + border-radius: 4px; + box-shadow: 4px 4px 12px 0 $menu-box-shadow-color; + background-color: $menu-bg-color; + color: $primary-fg-color; + position: absolute; + font-size: 14px; + z-index: 5001; +} + +.mx_InteractiveTooltip_right { + right: 0; +} + +.mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_right { + right: 8px; +} + +.mx_InteractiveTooltip_chevron_right { + position: absolute; + right: -8px; + top: 0px; + width: 0; + height: 0; + border-top: 8px solid transparent; + border-left: 8px solid $menu-bg-color; + border-bottom: 8px solid transparent; +} + +.mx_InteractiveTooltip_chevron_right::after { + content: ''; + width: 0; + height: 0; + border-top: 7px solid transparent; + border-left: 7px solid $menu-bg-color; + border-bottom: 7px solid transparent; + position: absolute; + top: -7px; + right: 1px; +} + +.mx_InteractiveTooltip_left { + left: 0; +} + +.mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_left { + left: 8px; +} + +.mx_InteractiveTooltip_chevron_left { + position: absolute; + left: -8px; + top: 0px; + width: 0; + height: 0; + border-top: 8px solid transparent; + border-right: 8px solid $menu-bg-color; + border-bottom: 8px solid transparent; +} + +.mx_InteractiveTooltip_chevron_left::after { + content: ''; + width: 0; + height: 0; + border-top: 7px solid transparent; + border-right: 7px solid $menu-bg-color; + border-bottom: 7px solid transparent; + position: absolute; + top: -7px; + left: 1px; +} + +.mx_InteractiveTooltip_top { + top: 0; +} + +.mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_top { + top: 8px; +} + +.mx_InteractiveTooltip_chevron_top { + position: absolute; + left: 0px; + top: -8px; + width: 0; + height: 0; + border-left: 8px solid transparent; + border-bottom: 8px solid $menu-bg-color; + border-right: 8px solid transparent; +} + +.mx_InteractiveTooltip_chevron_top::after { + content: ''; + width: 0; + height: 0; + border-left: 7px solid transparent; + border-bottom: 7px solid $menu-bg-color; + border-right: 7px solid transparent; + position: absolute; + left: -7px; + top: 1px; +} + +.mx_InteractiveTooltip_bottom { + bottom: 0; +} + +.mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_bottom { + bottom: 8px; +} + +.mx_InteractiveTooltip_chevron_bottom { + position: absolute; + left: 0px; + bottom: -8px; + width: 0; + height: 0; + border-left: 8px solid transparent; + border-top: 8px solid $menu-bg-color; + border-right: 8px solid transparent; +} + +.mx_InteractiveTooltip_chevron_bottom::after { + content: ''; + width: 0; + height: 0; + border-left: 7px solid transparent; + border-top: 7px solid $menu-bg-color; + border-right: 7px solid transparent; + position: absolute; + left: -7px; + bottom: 1px; +} diff --git a/src/components/views/elements/InteractiveTooltip.js b/src/components/views/elements/InteractiveTooltip.js new file mode 100644 index 00000000000..7c582a2b716 --- /dev/null +++ b/src/components/views/elements/InteractiveTooltip.js @@ -0,0 +1,141 @@ +/* +Copyright 2019 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import React from 'react'; +import ReactDOM from 'react-dom'; +import PropTypes from 'prop-types'; +import classNames from 'classnames'; + +const InteractiveTooltipContainerId = "mx_InteractiveTooltip_Container"; + +function getOrCreateContainer() { + let container = document.getElementById(InteractiveTooltipContainerId); + + if (!container) { + container = document.createElement("div"); + container.id = InteractiveTooltipContainerId; + document.body.appendChild(container); + } + + return container; +} + +export default class InteractiveTooltip extends React.Component { + propTypes: { + top: PropTypes.number, + bottom: PropTypes.number, + left: PropTypes.number, + right: PropTypes.number, + chevronOffset: PropTypes.number, + chevronFace: PropTypes.string, // top, bottom, left, right or none + // Function to be called on menu close + onFinished: PropTypes.func, + + // If true, insert an invisible screen-sized element behind the + // menu that when clicked will close it. + hasBackground: PropTypes.bool, + + // The component to render as the context menu + elementClass: PropTypes.element.isRequired, + // on resize callback + windowResize: PropTypes.func, + // method to close menu + closeTooltip: PropTypes.func, + }; + + render() { + const position = {}; + let chevronFace = null; + const props = this.props; + + if (props.top) { + position.top = props.top; + } else { + position.bottom = props.bottom; + } + + if (props.left) { + position.left = props.left; + chevronFace = 'left'; + } else { + position.right = props.right; + chevronFace = 'right'; + } + + const chevronOffset = {}; + if (props.chevronFace) { + chevronFace = props.chevronFace; + } + const hasChevron = chevronFace && chevronFace !== "none"; + + if (chevronFace === 'top' || chevronFace === 'bottom') { + chevronOffset.left = props.chevronOffset; + } else { + chevronOffset.top = props.chevronOffset; + } + + const chevron = hasChevron ? +
: + undefined; + const className = 'mx_InteractiveTooltip_wrapper'; + + const menuClasses = classNames({ + 'mx_InteractiveTooltip': true, + 'mx_InteractiveTooltip_left': !hasChevron && position.left, + 'mx_InteractiveTooltip_right': !hasChevron && position.right, + 'mx_InteractiveTooltip_top': !hasChevron && position.top, + 'mx_InteractiveTooltip_bottom': !hasChevron && position.bottom, + 'mx_InteractiveTooltip_withChevron_left': chevronFace === 'left', + 'mx_InteractiveTooltip_withChevron_right': chevronFace === 'right', + 'mx_InteractiveTooltip_withChevron_top': chevronFace === 'top', + 'mx_InteractiveTooltip_withChevron_bottom': chevronFace === 'bottom', + }); + + const ElementClass = props.elementClass; + + return
+
+ { chevron } + +
+ { props.hasBackground &&
} +
; + } +} + +export function createTooltip(ElementClass, props, hasBackground=true) { + const closeTooltip = function(...args) { + ReactDOM.unmountComponentAtNode(getOrCreateContainer()); + + if (props && props.onFinished) { + props.onFinished.apply(null, args); + } + }; + + // We only reference closeTooltip once per call to createTooltip + const menu = ; + + ReactDOM.render(menu, getOrCreateContainer()); + + return {close: closeTooltip}; +} From 32bf4588dd3fa38c0169d9b1594de6e8fec39603 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 20 Jun 2019 18:33:45 +0100 Subject: [PATCH 2/4] Center tooltip along top or bottom of target This adjusts the positioning to work more the way we want: * Tooltip is position on the top or bottom edge of the target depending on where space is available * Tooltip and chevron are centered In addition, more bits borrowed from `ContextualMenu` are not needed, so they have been removed for simplicity. Part of https://github.com/vector-im/riot-web/issues/9753 Part of https://github.com/vector-im/riot-web/issues/9716 --- .../views/elements/_InteractiveTooltip.scss | 74 +-------------- .../views/elements/InteractiveTooltip.js | 90 ++++++++++--------- 2 files changed, 49 insertions(+), 115 deletions(-) diff --git a/res/css/views/elements/_InteractiveTooltip.scss b/res/css/views/elements/_InteractiveTooltip.scss index 11f548fa180..3ec20be9280 100644 --- a/res/css/views/elements/_InteractiveTooltip.scss +++ b/res/css/views/elements/_InteractiveTooltip.scss @@ -39,79 +39,13 @@ limitations under the License. z-index: 5001; } -.mx_InteractiveTooltip_right { - right: 0; -} - -.mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_right { - right: 8px; -} - -.mx_InteractiveTooltip_chevron_right { - position: absolute; - right: -8px; - top: 0px; - width: 0; - height: 0; - border-top: 8px solid transparent; - border-left: 8px solid $menu-bg-color; - border-bottom: 8px solid transparent; -} - -.mx_InteractiveTooltip_chevron_right::after { - content: ''; - width: 0; - height: 0; - border-top: 7px solid transparent; - border-left: 7px solid $menu-bg-color; - border-bottom: 7px solid transparent; - position: absolute; - top: -7px; - right: 1px; -} - -.mx_InteractiveTooltip_left { - left: 0; -} - -.mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_left { - left: 8px; -} - -.mx_InteractiveTooltip_chevron_left { - position: absolute; - left: -8px; - top: 0px; - width: 0; - height: 0; - border-top: 8px solid transparent; - border-right: 8px solid $menu-bg-color; - border-bottom: 8px solid transparent; -} - -.mx_InteractiveTooltip_chevron_left::after { - content: ''; - width: 0; - height: 0; - border-top: 7px solid transparent; - border-right: 7px solid $menu-bg-color; - border-bottom: 7px solid transparent; - position: absolute; - top: -7px; - left: 1px; -} - -.mx_InteractiveTooltip_top { - top: 0; -} - .mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_top { top: 8px; } .mx_InteractiveTooltip_chevron_top { position: absolute; - left: 0px; + left: calc(50% - 8px); top: -8px; width: 0; height: 0; @@ -132,17 +66,13 @@ limitations under the License. top: 1px; } -.mx_InteractiveTooltip_bottom { - bottom: 0; -} - .mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_bottom { bottom: 8px; } .mx_InteractiveTooltip_chevron_bottom { position: absolute; - left: 0px; + left: calc(50% - 8px); bottom: -8px; width: 0; height: 0; diff --git a/src/components/views/elements/InteractiveTooltip.js b/src/components/views/elements/InteractiveTooltip.js index 7c582a2b716..b3d0b32fa74 100644 --- a/src/components/views/elements/InteractiveTooltip.js +++ b/src/components/views/elements/InteractiveTooltip.js @@ -33,21 +33,19 @@ function getOrCreateContainer() { return container; } +/* + * This style of tooltip takes a `target` element's rect and centers the tooltip + * along one edge of the target. + */ export default class InteractiveTooltip extends React.Component { propTypes: { - top: PropTypes.number, - bottom: PropTypes.number, - left: PropTypes.number, - right: PropTypes.number, - chevronOffset: PropTypes.number, - chevronFace: PropTypes.string, // top, bottom, left, right or none + // A DOMRect from the target element + targetRect: PropTypes.object.isRequired, // Function to be called on menu close onFinished: PropTypes.func, - // If true, insert an invisible screen-sized element behind the // menu that when clicked will close it. hasBackground: PropTypes.bool, - // The component to render as the context menu elementClass: PropTypes.element.isRequired, // on resize callback @@ -56,58 +54,64 @@ export default class InteractiveTooltip extends React.Component { closeTooltip: PropTypes.func, }; + constructor() { + super(); + + this.state = { + contentRect: null, + }; + } + + collectContentRect = (element) => { + // We don't need to clean up when unmounting, so ignore + if (!element) return; + + this.setState({ + contentRect: element.getBoundingClientRect(), + }); + } + render() { - const position = {}; - let chevronFace = null; const props = this.props; + const { targetRect } = props; - if (props.top) { - position.top = props.top; - } else { - position.bottom = props.bottom; - } + // The window X and Y offsets are to adjust position when zoomed in to page + const targetLeft = targetRect.left + window.pageXOffset; + const targetBottom = targetRect.bottom + window.pageYOffset; + const targetTop = targetRect.top + window.pageYOffset; - if (props.left) { - position.left = props.left; - chevronFace = 'left'; + // Align the tooltip vertically on whichever side of the target has more + // space available. + const position = {}; + let chevronFace = null; + if (targetBottom < window.innerHeight / 2) { + position.top = targetBottom; + chevronFace = "top"; } else { - position.right = props.right; - chevronFace = 'right'; + position.bottom = window.innerHeight - targetTop; + chevronFace = "bottom"; } - const chevronOffset = {}; - if (props.chevronFace) { - chevronFace = props.chevronFace; - } - const hasChevron = chevronFace && chevronFace !== "none"; + // Center the tooltip horizontally with the target's center. + position.left = targetLeft + targetRect.width / 2; - if (chevronFace === 'top' || chevronFace === 'bottom') { - chevronOffset.left = props.chevronOffset; - } else { - chevronOffset.top = props.chevronOffset; - } - - const chevron = hasChevron ? -
: - undefined; - const className = 'mx_InteractiveTooltip_wrapper'; + const chevron =
; const menuClasses = classNames({ 'mx_InteractiveTooltip': true, - 'mx_InteractiveTooltip_left': !hasChevron && position.left, - 'mx_InteractiveTooltip_right': !hasChevron && position.right, - 'mx_InteractiveTooltip_top': !hasChevron && position.top, - 'mx_InteractiveTooltip_bottom': !hasChevron && position.bottom, - 'mx_InteractiveTooltip_withChevron_left': chevronFace === 'left', - 'mx_InteractiveTooltip_withChevron_right': chevronFace === 'right', 'mx_InteractiveTooltip_withChevron_top': chevronFace === 'top', 'mx_InteractiveTooltip_withChevron_bottom': chevronFace === 'bottom', }); + const menuStyle = {}; + if (this.state.contentRect) { + menuStyle.left = `-${this.state.contentRect.width / 2}px`; + } + const ElementClass = props.elementClass; - return
-
+ return
+
{ chevron }
From 3bd247ebaa02acf973f992cb6c01b221cd03c90f Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Fri, 21 Jun 2019 11:41:19 +0100 Subject: [PATCH 3/4] Tweak interactive tooltip to match design This tweaks the tooltip to match the color, spacing, etc. seen in the designs. Part of https://github.com/vector-im/riot-web/issues/9753 Part of https://github.com/vector-im/riot-web/issues/9716 --- .../views/elements/_InteractiveTooltip.scss | 43 +++++-------------- res/themes/dark/css/_dark.scss | 3 ++ res/themes/light/css/_light.scss | 3 ++ 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/res/css/views/elements/_InteractiveTooltip.scss b/res/css/views/elements/_InteractiveTooltip.scss index 3ec20be9280..a949941dd89 100644 --- a/res/css/views/elements/_InteractiveTooltip.scss +++ b/res/css/views/elements/_InteractiveTooltip.scss @@ -30,17 +30,18 @@ limitations under the License. } .mx_InteractiveTooltip { - border-radius: 4px; - box-shadow: 4px 4px 12px 0 $menu-box-shadow-color; - background-color: $menu-bg-color; - color: $primary-fg-color; + border-radius: 3px; + background-color: $interactive-tooltip-bg-color; + color: $interactive-tooltip-fg-color; position: absolute; - font-size: 14px; + font-size: 10px; + font-weight: 600; + padding: 6px; z-index: 5001; } .mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_top { - top: 8px; + top: 10px; // 8px chevron + 2px spacing } .mx_InteractiveTooltip_chevron_top { @@ -50,24 +51,12 @@ limitations under the License. width: 0; height: 0; border-left: 8px solid transparent; - border-bottom: 8px solid $menu-bg-color; + border-bottom: 8px solid $interactive-tooltip-bg-color; border-right: 8px solid transparent; } -.mx_InteractiveTooltip_chevron_top::after { - content: ''; - width: 0; - height: 0; - border-left: 7px solid transparent; - border-bottom: 7px solid $menu-bg-color; - border-right: 7px solid transparent; - position: absolute; - left: -7px; - top: 1px; -} - .mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_bottom { - bottom: 8px; + bottom: 10px; // 8px chevron + 2px spacing } .mx_InteractiveTooltip_chevron_bottom { @@ -77,18 +66,6 @@ limitations under the License. width: 0; height: 0; border-left: 8px solid transparent; - border-top: 8px solid $menu-bg-color; + border-top: 8px solid $interactive-tooltip-bg-color; border-right: 8px solid transparent; } - -.mx_InteractiveTooltip_chevron_bottom::after { - content: ''; - width: 0; - height: 0; - border-left: 7px solid transparent; - border-top: 7px solid $menu-bg-color; - border-right: 7px solid transparent; - position: absolute; - left: -7px; - bottom: 1px; -} diff --git a/res/themes/dark/css/_dark.scss b/res/themes/dark/css/_dark.scss index bdccf715409..ed1cc162a09 100644 --- a/res/themes/dark/css/_dark.scss +++ b/res/themes/dark/css/_dark.scss @@ -160,6 +160,9 @@ $reaction-row-button-selected-border-color: $accent-color; $tooltip-timeline-bg-color: $tagpanel-bg-color; $tooltip-timeline-fg-color: #ffffff; +$interactive-tooltip-bg-color: $base-color; +$interactive-tooltip-fg-color: #ffffff; + // ***** Mixins! ***** @define-mixin mx_DialogButton { diff --git a/res/themes/light/css/_light.scss b/res/themes/light/css/_light.scss index 8244485ee31..361f6fa408d 100644 --- a/res/themes/light/css/_light.scss +++ b/res/themes/light/css/_light.scss @@ -272,6 +272,9 @@ $reaction-row-button-selected-border-color: $accent-color; $tooltip-timeline-bg-color: $tagpanel-bg-color; $tooltip-timeline-fg-color: #ffffff; +$interactive-tooltip-bg-color: #27303a; +$interactive-tooltip-fg-color: #ffffff; + // ***** Mixins! ***** @define-mixin mx_DialogButton { From 80d73d84308411638681da11342c6c3d88c6f3e6 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Fri, 21 Jun 2019 13:48:20 +0100 Subject: [PATCH 4/4] Add optional rounded chevron for tooltip We'd like to have a rounded point on the chevron for an extra level of polish. This implements that look for browsers that support `clip-path`. Part of https://github.com/vector-im/riot-web/issues/9716 --- .../views/elements/_InteractiveTooltip.scss | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/res/css/views/elements/_InteractiveTooltip.scss b/res/css/views/elements/_InteractiveTooltip.scss index a949941dd89..a3f5b6edc2f 100644 --- a/res/css/views/elements/_InteractiveTooltip.scss +++ b/res/css/views/elements/_InteractiveTooltip.scss @@ -55,6 +55,21 @@ limitations under the License. border-right: 8px solid transparent; } +// Adapted from https://codyhouse.co/blog/post/css-rounded-triangles-with-clip-path +// by Sebastiano Guerriero (@guerriero_se) +@supports (clip-path: polygon(0% 0%, 100% 100%, 0% 100%)) { + .mx_InteractiveTooltip_chevron_top { + height: 16px; + width: 16px; + background-color: inherit; + border: none; + clip-path: polygon(0% 0%, 100% 100%, 0% 100%); + transform: rotate(135deg); + border-radius: 0 0 0 3px; + top: calc(-8px / 1.414); // sqrt(2) because of rotation + } +} + .mx_InteractiveTooltip.mx_InteractiveTooltip_withChevron_bottom { bottom: 10px; // 8px chevron + 2px spacing } @@ -69,3 +84,18 @@ limitations under the License. border-top: 8px solid $interactive-tooltip-bg-color; border-right: 8px solid transparent; } + +// Adapted from https://codyhouse.co/blog/post/css-rounded-triangles-with-clip-path +// by Sebastiano Guerriero (@guerriero_se) +@supports (clip-path: polygon(0% 0%, 100% 100%, 0% 100%)) { + .mx_InteractiveTooltip_chevron_bottom { + height: 16px; + width: 16px; + background-color: inherit; + border: none; + clip-path: polygon(0% 0%, 100% 100%, 0% 100%); + transform: rotate(-45deg); + border-radius: 0 0 0 3px; + bottom: calc(-8px / 1.414); // sqrt(2) because of rotation + } +}