Skip to content

Commit

Permalink
Tooltips for markdown fields
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Jan 23, 2024
1 parent eb50c53 commit c531c08
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/formelements.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Fieldsets group other form elements (including nested fieldsets).
Options:
* `children` _(required)_ - containing child form elements
* `background` _(optional)_ the background color may be one of [https://bulma.io/documentation/helpers/color-helpers/#background-color](Bulma's colors) or a valid CSS definition.
* `background` _(optional)_ the background color may be one of [https://bulma.io/documentation/helpers/color-helpers/#background-color](Bulma's colors) (without the prefix `has-background-`) or a valid CSS definition.
* `tablestyle` _(optional)_ set table view true or false. This will order the contained fieldsets as if they were table columns. The columns will have alternating colors ("zebra"). The head column will be populated by the labels of the children of the first fieldset. Labels in other columns will be hidden to avoid duplication. If cells in the first row must be skipped then [Spacers](#Spacer) can be used.
* `scrollable` _(optional)_ can be used with `tablestyle`: The fieldset will not responsively adapt to screen width, but will extend horizontally and can be scrolled. The option may have unpredictable effects in complex forms!
* `toggle` _(optional)_ - the fieldset is disabled and hidden until the toggle condition is met
Expand Down
11 changes: 11 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,14 @@ fieldset .subtitle {
border: 3px solid #fff;
background-color: rgba(0, 0, 0, .3);
}

div .field.with-tooltip .control {
display: inline-flex;
justify-content: space-between;
align-items: normal;
align-content: baseline;

& button {
margin-left: 0.5rem;
}
}
11 changes: 11 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,14 @@ function cloneHandler(e) {
}

form.addEventListener('click', cloneHandler);

/**
* Move tooltips inside headings for better styling
*/
Array.from(document.querySelectorAll('.with-tooltip .control button.tooltip-button')).forEach(function(elem) {
const prev = elem.previousElementSibling;
const headlines = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
if (headlines.includes(prev.tagName)) {
prev.appendChild(elem);
}
});
14 changes: 0 additions & 14 deletions src/FormGenerator/FormElements/AbstractDynamicFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,4 @@ public function getViewVariables()
]
);
}

/**
* Parses tooltip.
* Transform newlines (\n) to NCR representation.
* If no tooltip given return empty string.
*
* @return string
*/
protected function parseTooltip()
{
$tooltip = $this->getConfigValue('tooltip') ?? '';

return str_replace("\n", '

', $tooltip);
}
}
14 changes: 14 additions & 0 deletions src/FormGenerator/FormElements/AbstractFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public function hasParent()
return $this->parent !== null;
}

/**
* Parses tooltip.
* Transform newlines (\n) to NCR representation.
* If no tooltip given return empty string.
*
* @return string
*/
protected function parseTooltip()
{
$tooltip = $this->getConfigValue('tooltip') ?? '';

return str_replace("\n", '

', $tooltip);
}

/**
* Prepare variables array for twig view
*
Expand Down
3 changes: 2 additions & 1 deletion src/FormGenerator/FormElements/MarkDownFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function getViewVariables()
$this->getConfig(),
[
'id' => $this->getFormElementId(),
'markdown' => $markdown
'markdown' => $markdown,
'tooltip' => $this->parseTooltip(),
]
);
}
Expand Down
4 changes: 3 additions & 1 deletion view/markdown.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{% from '_macros' import renderTooltip %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="field with-tooltip">
<div class="control">
{{ markdown | raw }}
{{ renderTooltip(tooltip) }}
</div>
</div>
</div>

0 comments on commit c531c08

Please sign in to comment.