Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 62 additions & 75 deletions docs/badges_buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,83 +53,11 @@ The syntax is the same as for the `ref` role.
````
`````

(sd-custom-badge-roles)=

### Custom badge roles

When the **same** badge recurs throughout your documentation — status labels
such as *stable*, *beta* or *deprecated* whose colour and tooltip never change —
define it **once** as a custom role via the `sd_custom_roles` configuration
option, rather than retyping the colour and tooltip at every use site. This is
the recommended pattern for such repeated, semantic badges:

```python
sd_custom_roles = {
"bdg-stable": {
"inherit": "bdg-success",
"tooltip": "A released, supported version",
},
"bdg-beta": {"inherit": "bdg-warning", "tooltip": "Interface may change"},
"bdg-deprecated": {"inherit": "bdg-danger", "tooltip": "Scheduled for removal"},
}
```

Each key is the new role name to register; the value is a dictionary with:

- `inherit` (required): the built-in badge role to inherit from — any of the
`bdg`, `bdg-<color>`, `bdg-<color>-line`, `bdg-link-*` or `bdg-ref-*` roles.
- `tooltip` (optional): a default tooltip, applied as the HTML `title` whenever
the role is used *without* a per-instance `; tooltip` suffix. For a
`bdg-link-*`/`bdg-ref-*` role the baked tooltip applies even to the bare
form (`` {bdg-link-*}`<target>` ``), where the suffix grammar deliberately
refuses to split, and it overrides a reference's automatic title.

Give custom roles a distinguishing prefix (the `bdg-` convention used here is a
good choice): a name that collides with a docutils built-in (`code`, `math`,
`strong`, …) or another extension's role is skipped, so `bdg-`-prefixed names
keep you clear of every such clash.

The new role then behaves exactly like the role it inherits (same rendering,
links and references), with its tooltip baked in:

{bdg-stable}`v2.1` {bdg-beta}`v3.0rc1` {bdg-deprecated}`v1.0`

````{tab-set-code}
```markdown
{bdg-stable}`v2.1` {bdg-beta}`v3.0rc1` {bdg-deprecated}`v1.0`
```
```rst
:bdg-stable:`v2.1` :bdg-beta:`v3.0rc1` :bdg-deprecated:`v1.0`
```
````

A baked `tooltip` is only the *default*: a per-instance `; tooltip` suffix on an
individual badge **overrides** it, so a config-defined role covers the common
case while the suffix stays available for the occasional one-off:

{bdg-stable}`v2.1 ; Long-term support release`

````{tab-set-code}
```markdown
{bdg-stable}`v2.1 ; Long-term support release`
```
```rst
:bdg-stable:`v2.1 ; Long-term support release`
```
````

Custom roles are the badge analogue of
{ref}`custom directives <sd-custom-directives>`. Inheritance is limited to the
badge family listed above; a role whose `inherit` names an unknown badge role,
or whose own name clashes with an existing role, is skipped with a warning.
Editing an entry (or removing it) is picked up on the next build.

### Badge tooltips

For a **one-off** tooltip (shown on hover, via the HTML `title` attribute) —
rather than a repeated one better served by a
{ref}`custom badge role <sd-custom-badge-roles>` — append a `; tooltip` suffix
to any badge's text. This works for every badge family:
Any badge can be given a tooltip (shown on hover, via the HTML `title`
attribute) by appending a `; tooltip` suffix to its text.
This works for every badge family:

{bdg-primary}`stable ; A released, supported version`
{bdg-link-info}`docs <https://example.com> ; Opens the documentation`
Expand Down Expand Up @@ -168,6 +96,65 @@ not surfaced by all screen readers. Do not put essential information in a
tooltip alone -- keep it in the visible badge text (or nearby prose) as well.
```

(sd-badge-tooltip-option)=

#### The `tooltip` role option

When the same badge recurs, such as a status label whose colour and tooltip never change,
its tooltip can be given once rather than in every use.
Every badge role accepts a `tooltip` role option, which is the badge's tooltip
when its text has no `; tooltip` suffix (a suffix still overrides it).
In reStructuredText, set it with the `role` directive, which defines a new role based on a badge role
for the document it appears in:

```rst
.. role:: bdg-stable(bdg-success)
:tooltip: A released, supported version

:bdg-stable:`v2.1` :bdg-stable:`v2.2 ; Long-term support release`
```

```{eval-rst}
.. role:: bdg-stable(bdg-success)
:tooltip: A released, supported version

:bdg-stable:`v2.1` :bdg-stable:`v2.2 ; Long-term support release`
```

To use such a role in every reStructuredText document,
put the `role` directive in [`rst_prolog`](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-rst_prolog)
in your `conf.py`, which is read at the start of each reStructuredText document
(Markdown documents parsed by MyST do not read it):

```python
rst_prolog = """
.. role:: bdg-stable(bdg-success)
:tooltip: A released, supported version
"""
```

Defining the role in only one document is not enough for the others:
a document that uses a role defined elsewhere can fail with an "Unknown interpreted text role" error,
for example in an incremental or parallel build.

The option also applies to the bare `<target>` form of the link and reference badges,
where the `; tooltip` suffix is not recognised.

For extension authors: the badge roles follow the docutils convention for role functions.
Their option specification is the role's `options` attribute (`{"tooltip": ...}`;
docutils also adds `class` when the role is registered, which badges accept and ignore),
and the option is read from the `options` dictionary passed to each call,
so an extension that presets a badge role passes `options={"tooltip": "..."}`.

:::{tip}
Role presets in the project configuration are planned for the
[sphinx-ub-directives](https://sphinx-ub-directives.readthedocs.io) package,
from the [sphinx-needs](https://github.com/useblocks/sphinx-needs) project,
whose directive presets already accept sphinx-design directives as a base
and also work in [ubCode](https://ubcode.useblocks.com/).
Until then, use `rst_prolog` as above.
:::

See [Bootstrap badges](https://getbootstrap.com/docs/5.0/components/badge/) for more information, and related [Material Design chips](https://material.io/components/chip).

(buttons)=
Expand Down
8 changes: 0 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@
},
}
}
sd_custom_roles = {
"bdg-stable": {
"inherit": "bdg-success",
"tooltip": "A released, supported version",
},
"bdg-beta": {"inherit": "bdg-warning", "tooltip": "Interface may change"},
"bdg-deprecated": {"inherit": "bdg-danger", "tooltip": "Scheduled for removal"},
}

extlinks = {
"pr": ("https://github.com/executablebooks/sphinx-design/pull/%s", "PR #%s"),
Expand Down
13 changes: 10 additions & 3 deletions docs/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,16 @@ The key is the new directive name to add, and the value is a dictionary with the
- `argument`: The default argument (optional, only for directives that take a single argument)
- `options`: A dictionary of default options for the directive (optional)

The same idea is available for roles: `sd_custom_roles` defines reusable badge
roles (e.g. status badges), inheriting a built-in badge role and optionally
baking in a tooltip. See {ref}`Custom badge roles <sd-custom-badge-roles>`.
:::{tip}
For presets beyond default option values,
see the [sphinx-ub-directives](https://sphinx-ub-directives.readthedocs.io) package,
from the [sphinx-needs](https://github.com/useblocks/sphinx-needs) project,
whose directive presets accept sphinx-design directives (such as `dropdown`) as a base.
It adds appended and forced option values, need-type bases (with sphinx-needs), and template directives,
and its presets can also be configured in `ubproject.toml`,
which [ubCode](https://ubcode.useblocks.com/) reads.
To preset a badge's tooltip, see the badge roles' {ref}`tooltip role option <sd-badge-tooltip-option>`.
:::

## Supported browsers

Expand Down
Loading
Loading