sphinx-design icon indicating copy to clipboard operation
sphinx-design copied to clipboard

Nesting of tabs and dropdowns etc

Open BarrySmith opened this issue 1 year ago • 1 comments
trafficstars

Context

Is it (would it be) possible to allow nesting of tabs, dropdowns etc?

I tried putting a dropdown inside a tab-item and get the error message:

WARNING: The parent of a 'tab-item' should be a 'tab-set' [design.tab]
/Users/barrysmith/Src/petsc/doc/objects.md:170: WARNING: All children of a 'tab-set' should be 'tab-item' [design.tab]

Proposal

Allowing nesting of tabs, dropdowns etc?

Tasks and updates

No response

BarrySmith avatar Aug 09 '24 18:08 BarrySmith

Heya yeh should be possible, can you provide a "minimal working example" of the warning being raised

chrisjsewell avatar Aug 09 '24 19:08 chrisjsewell

I know it's been a while since @BarrySmith first opened this thread, but here's an example. I'm using MyST Markdown instead of rST (not that it should matter for the end result), but this code...

::::::{dropdown} Manually created dropdown
This is some text before the tab-set.
:::::{tab-set}
::::{tab-item} Label for Tab 1
This is content for the 1st tab.
:::{dropdown} another dropdown for funsies
text
:::
::::
::::{tab-item} Label for Tab 2
This is content for the 2nd tab!
::::
:::::
::::::

...produces what I think is exactly what you're asking for:

image

So it's clearly possible to nest dropdowns and tabs in each other. You just need to make sure that you're nesting content inside a tab-item directive instead of its parent tab-set.

I do have a related question for you, though, @chrisjsewell. I seem to be able to do this correctly when I manually define nested dropdowns/tab set directives - but when I try to create a tab-set inside a dropdown using a custom directive, the text does not render correctly.

For example, I tried to create a similar dropdown to what I showed above - but using a locally-defined directive. Instead of getting tabs nested inside a dropdown (i.e. two dropdowns that look identical to each other), the contents of my auto-generated tab set just get rendered as plain text.

image

Here's a repo where I recreated this issue in an otherwise-bare-bones document; use this particular commit if necessary.

The only clue I have is these error messages:

DEBUG: Created tab 'Label for Tab 1' with content:
This is content for the 1st tab.
DEBUG: Created tab 'Label for Tab 2' with content:
This is content for the 2nd tab!
DEBUG: Created tab set with 2 tabs
DEBUG: Created dropdown: 'Automatically created dropdown'

[PATH REDACTED]/doc-test/source/index.md:19: WARNING: The parent of a 'tab-item' should be a 'tab-set' [design.tab] [design.tab]
[PATH REDACTED]/doc-test/source/index.md:19: WARNING: The parent of a 'tab-item' should be a 'tab-set' [design.tab] [design.tab]
[PATH REDACTED]/doc-test/source/index.md:22: WARNING: All children of a 'tab-set' should be 'tab-item' [design.tab] [design.tab]

I'm confused by this, though, since the contents of make_node_tabset are defined as string representations of my tab-item directives. And this seems to be the only way that I can build the doc to completion; if I keep self.content as a list of nodes, I get the following exception:

DEBUG: Created tab 'Label for Tab 2' with content:
This is content for the 2nd tab!
[PATH REDACTED]/doc-test/source/index.md:19: WARNING: The parent of a 'tab-item' should be a 'tab-set' [design.tab] [design.tab]
[PATH REDACTED]/doc-test/source/index.md:19: WARNING: The parent of a 'tab-item' should be a 'tab-set' [design.tab] [design.tab]

Traceback (most recent call last):
[parts of traceback omitted for brevity]
  File "[PATH REDACTED]/doc-test/source/_ext/enforcer.py", line 101, in run
    node = make_node_dropdown(self, name_arg)
  File "[PATH REDACTED]/doc-test/source/_ext/enforcer.py", line 68, in make_node_dropdown
    node_tabs = make_node_tabset(self, standard_want)
  File "[PATH REDACTED]/doc-test/source/_ext/enforcer.py", line 52, in make_node_tabset
    node = TabSetDirective.run_with_defaults(self)
  File "/opt/miniconda3/envs/[CONDA ENV NAME REDACTED]/lib/python3.13/site-packages/sphinx_design/tabs.py", line 37, in run_with_defaults
    self.state.nested_parse(self.content, self.content_offset, tab_set)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/[CONDA ENV NAME REDACTED]/lib/python3.13/site-packages/myst_parser/mocking.py", line 174, in nested_parse
    "\n".join(block),
    ~~~~~~~~~^^^^^^^
TypeError: sequence item 0: expected str instance, container found

Exception occurred:
  File "/opt/miniconda3/envs/[CONDA ENV NAME REDACTED]/lib/python3.13/site-packages/myst_parser/mocking.py", line 174, in nested_parse
    "\n".join(block),
    ~~~~~~~~~^^^^^^^
TypeError: sequence item 0: expected str instance, container found

This reads to me (a Sphinx noob) as a damned-if-you-do-damned-if-you-don't situation. Any suggestions?

keitaay avatar Jan 14 '25 14:01 keitaay