1.0.1 • Published 4 months ago

solid-js-tooltip v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

solid-js-tooltip

pnpm

Quick start

Installation:

npm i solid-js-tooltip
# or
yarn solid-js-tooltip
# or
pnpm add solid-js-tooltip

Demo here!


Setup:

import { type TooltipDirective, tooltip } from 'solid-js-tooltip';
import 'solid-js-tooltip/styles.css';

// https://github.com/solidjs/solid/discussions/845
tooltip;

declare module 'solid-js' {
  namespace JSX {
    interface Directives extends TooltipDirective {}
  }
}

Examples:

import { tooltip, Tooltip } from 'solid-js-tooltip';

...

<p
  use:tooltip={{
    tooltips: [
      {
        element: (
          <Tooltip class="tooltip">
            What...
          </Tooltip>
        ),
        position: 'top-center',
      },
    ],
  }}
  tabIndex={0}
>
  This is bla, bla, bla and bla...
</p>
import { tooltip, Tooltip } from 'solid-js-tooltip';

...

<p
  use:tooltip={{
    tooltips: [
      {
        element: (
          <Tooltip class="tooltip">
            What...
          </Tooltip>
        ),
        position: 'top-center',
      },
      {
        element: (
          <Tooltip class="tooltip">
            is ... this...
          </Tooltip>
        ),
        position: 'right-center',
      },
    ],
  }}
  tabIndex={0}
>
  Etiam dictum eleifend justo, sit amet porttitor lectus ullamcorper eget. Morbi
  aliquet, nibh non porta euismod, metus est tincidunt ex, id vehicula massa
  metus id arcu. Nunc quis tincidunt metus, eu dapibus ligula.
</p>
import { tooltip, Tooltip } from 'solid-js-tooltip';

...

<p
  class="highlight-text"
  use:tooltip={{
    tooltips: [
      {
        element: (
          <Tooltip class="tooltip">
            Hey! I am describing something...
          </Tooltip>
        ),
        displayOnHover: false,
      },
    ],
    onFocusin: (event) => {
      console.log('"focusin" event:"', event);
    },
  }}
  tabIndex={0}
>
  Maecenas blandit arcu eget rutrum sodales. Vestibulum tempor mi nec metus
  elementum fermentum. Aenean a gravida justo, nec pharetra massa.
</p>
import { type TooltipComponent, Tooltip as BaseTooltip } from 'solid-js-tooltip';
import 'styles.css';

// You can use "Tooltip" component as base for your custom tooltip.
export const Tooltip: TooltipComponent = (props) => {
  return (
    <BaseTooltip {...props} class={`${props?.class || ''} tooltip`}>
      <div>
        <div>
          ...
        </div>
      </div>
    </BaseTooltip>
  )
};

...

import { tooltip } from 'solid-js-tooltip';
import { Tooltip } from './custom-tooltip-component-from-somewhere';

<p
  use:tooltip={{
    tooltips: [
      {
        element: (
          <Tooltip>
            Hey! I am describing something...
          </Tooltip>
        ),
        displayOnHover: false,
      },
    ],
    onFocusin: (event) => {
      console.log('"focusin" event:"', event);
    },
  }}
  tabIndex={0}
>
  Lorem ipsum dolor sit amet consectetur adipisicing elit. Eaque, suscipit?
</p>

Component API:

Tooltip component has the same attributes and events as HTMLDivElement and serves as a base for your custom tooltip.

Default props:

Prop nameTypeDescriptionDefault value
classstringread on MDN'solid-js-tooltip'
rolestringread on MDN'tooltip'
tabindexnumberread on MDN-1
inertbooleanread on MDNtrue
aria-labelstringread on MDN'tooltip'
aria-labelledbystringread on MDN'tooltip'
aria-hiddenbooleanread on MDNtrue
data-tooltip-sr-notificationstringData attribute that notifies the screen reader user that this element has a tooltip.'; Has tooltip: '

Directive API:

Option nameTypeDescription
tooltipsobjectArray of options for each defined tooltip.
onMouseEnterfunction or undefinedEvent that occurs when the mouse pointer enters an element. (reference)
onMouseLeavefunction or undefinedEvent that occurs when the mouse pointer leaves an element. (reference)
onFocusInfunction or undefinedEvent that occurs when an element gets focus. (reference)
onFocusOutfunction or undefinedEvent that occurs when an element loses focus. (reference)

tooltips option:

Option nameTypeDescription
elementobjectElement used as a tooltip.
positiontop-left-corner or top-left or top-center or top-right or top-right-corner or right-top or right-center or right-bottom or bottom-right-corner or bottom-right or bottom-center or bottom-left or bottom-left-corner or left-bottom or left-center or left-topTooltip position. By default it is top-left position.
displayOnHoverboolean or undefinedControls whether a tooltip is displayed when hovering over an element. Bu default is is true.
displayOnFocusboolean or undefinedControls whether a tooltip is displayed when focusing over an element. Bu default is is true.
1.0.1

4 months ago

1.0.0

4 months ago