css.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. CSS Patterns
  2. ------------
  3. This document describes the patterns we are using to organize and write
  4. CSS for JupyterLab. JupyterLab is developed using a set of npm packages
  5. that are located in ``packages``. Each of these packages has its own
  6. style, but depend on CSS variables defined in a main theme package.
  7. CSS checklist
  8. ~~~~~~~~~~~~~
  9. - CSS classnames are defined inline in the code. We used to put them as
  10. all caps file-level ``const``\ s, but we are moving away from that.
  11. - CSS files for packages are located within the ``style``
  12. subdirectory and imported into the plugin's ``index.css``.
  13. - The JupyterLab default CSS variables in the ``theme-light-extension``
  14. and ``theme-dark-extension`` packages are used to style packages
  15. where ever possible. Individual packages should not npm-depend on
  16. these packages though, to enable the theme to be swapped out.
  17. - Additional public/private CSS variables are defined by plugins
  18. sparingly and in accordance with the conventions described below.
  19. CSS variables
  20. ~~~~~~~~~~~~~
  21. We are using native CSS variables in JupyterLab. This is to enable
  22. dynamic theming of built-in and third party plugins. As of December
  23. 2017, CSS variables are supported in the latest stable versions of all
  24. popular browsers, except for IE. If a JupyterLab deployment needs to
  25. support these browsers, a server side CSS preprocessor such as Myth or
  26. cssnext may be used.
  27. Naming of CSS variables
  28. ^^^^^^^^^^^^^^^^^^^^^^^
  29. We use the following convention for naming CSS variables:
  30. - Start all CSS variables with ``--jp-``.
  31. - Words in the variable name should be lowercase and separated with
  32. ``-``.
  33. - The next segment should refer to the component and subcomponent, such
  34. as ``--jp-notebook-cell-``.
  35. - The next segment should refer to any state modifiers such as
  36. ``active``, ``not-active`` or ``focused``:
  37. ``--jp-notebook-cell-focused``.
  38. - The final segment will typically be related to a CSS properties, such
  39. as ``color``, ``font-size`` or ``background``:
  40. ``--jp-notebook-cell-focused-background``.
  41. Public/private
  42. ^^^^^^^^^^^^^^
  43. Some CSS variables in JupyterLab are considered part of our public API.
  44. Others are considered private and should not be used by third party
  45. plugins or themes. The difference between public and private variables
  46. is simple:
  47. - All private variables begin with ``--jp-private-``
  48. - All variables without the ``private-`` prefix are public.
  49. - Public variables should be defined under the ``:root``
  50. pseudo-selector. This ensures that public CSS variables can be
  51. inspected under the top-level ``<html>`` tag in the browser's dev
  52. tools.
  53. - Where possible, private variables should be defined and scoped under
  54. an appropriate selector other than ``:root``.
  55. CSS variable usage
  56. ^^^^^^^^^^^^^^^^^^
  57. JupyterLab includes a default set of CSS variables in the file:
  58. ``packages/theme-light-extension/style/variables.css``
  59. To ensure consistent design in JupyterLab, all built-in and third party
  60. extensions should use these variables in their styles if at all
  61. possible. Documentation about those variables can be found in the
  62. ``variables.css`` file itself.
  63. Plugins are free to define additional public and private CSS variables
  64. in their own ``index.css`` file, but should do so sparingly.
  65. Again, we consider the names of the public CSS variables in this package
  66. to be our public API for CSS.
  67. File organization
  68. ~~~~~~~~~~~~~~~~~
  69. We are organizing our CSS files in the following manner:
  70. - Each package in the top-level ``packages`` directory should contain
  71. any CSS files in a ``style`` subdirectory that are needed to style
  72. itself.
  73. - All local styles should be consolidated into a ``style/base.css`` file.
  74. - The top level ``index.css`` file is templated by ``buildutils`` as
  75. part of the ``integrity`` script. It imports the CSS in dependency order,
  76. ending with the local ``./base.css``. CSS from external libraries is
  77. determined by their ``style`` field in ``package.json``. If additional
  78. files are desired or the external library does not have a ``style`` field,
  79. we use the ``jupyterlab: { "extraStyles": { "fooLibrary": ["path/to/css"] } }``
  80. pattern in our ``package.json`` to declare them. For imports that should not be added to ``index.css`, update ``SKIP_CSS`` in ``buildutils/src/ensure-repo.ts``.
  81. CSS class names
  82. ~~~~~~~~~~~~~~~
  83. CSS class naming conventions
  84. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  85. We have a fairly formal method for naming our CSS classes.
  86. First, CSS class names are associated with TypeScript classes that
  87. extend ``phosphor.Widget``:
  88. The ``.node`` of each such widget should have a CSS class that matches
  89. the name of the TypeScript class:
  90. .. code:: TypeScript
  91. class MyWidget extends Widget {
  92. constructor() {
  93. super();
  94. this.addClass('jp-MyWidget');
  95. }
  96. }
  97. Second, subclasses should have a CSS class for both the parent and
  98. child:
  99. .. code:: TypeScript
  100. class MyWidgetSubclass extends MyWidget {
  101. constructor() {
  102. super(); // Adds `jp-MyWidget`
  103. this.addClass('jp-MyWidgetSubclass');
  104. }
  105. }
  106. In both of these cases, CSS class names with caps-case are reserved for
  107. situations where there is a named TypeScript ``Widget`` subclass. These
  108. classes are a way of a TypeScript class providing a public API for
  109. styling.
  110. Third, children nodes of a ``Widget`` should have a third segment in the
  111. CSS class name that gives a semantic naming of the component, such as:
  112. - ``jp-MyWidget-toolbar``
  113. - ``jp-MyWidget-button``
  114. - ``jp-MyWidget-contentButton``
  115. In general, the parent ``MyWidget`` should add these classes to the
  116. children. This applies when the children are plain DOM nodes or
  117. ``Widget`` instances/subclasses themselves. Thus, the general naming of
  118. CSS classes is of the form ``jp-WidgetName-semanticChild``. This enables
  119. the styling of these children in a manner that is independent of the
  120. children implementation or CSS classes they have themselves.
  121. Fourth, some CSS classes are used to modify the state of a widget:
  122. - ``jp-mod-active``: applied to elements in the active state
  123. - ``jp-mod-hover``: applied to elements in the hover state
  124. - ``jp-mod-selected``: applied to elements while selected
  125. Fifth, some CSS classes are used to distinguish different types of a
  126. widget:
  127. - ``jp-type-separator``: applied to menu items that are separators
  128. - ``jp-type-directory``: applied to elements in the file browser that
  129. are directories
  130. Edge cases
  131. ^^^^^^^^^^
  132. Over time, we have found that there are some edge cases that these rules
  133. don't fully address. Here, we try to clarify those edge cases.
  134. **When should a parent add a class to children?**
  135. Above, we state that a parent (``MyWidget``), should add CSS classes to
  136. children that indicate the semantic function of the child. Thus, the
  137. ``MyWidget`` subclass of ``Widget`` should add ``jp-MyWidget`` to itself
  138. and ``jp-MyWidget-toolbar`` to a toolbar child.
  139. What if the child itself is a ``Widget`` and already has a proper CSS
  140. class name itself, such as ``jp-Toolbar``? Why not use selectors such as
  141. ``.jp-MyWidget .jp-Toolbar`` or ``.jp-MyWidget > .jp-Toolbar``?
  142. The reason is that these selectors are dependent on the implementation
  143. of the toolbar having the ``jp-Toolbar`` CSS class. When ``MyWidget``
  144. adds the ``jp-MyWidget-toolbar`` class, it can style the child
  145. independent of its implementation. The other reason to add the
  146. ``jp-MyWidget-toolbar`` class is if the DOM structure is highly
  147. recursive, the usual descendant selectors may not be specific to target
  148. only the desired children.
  149. When in doubt, there is little harm done in parents adding selectors to
  150. children.
  151. Commonly used CSS selectors
  152. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  153. We use CSS selectors to decide which context menu items to display and what command
  154. to invoke when a keyboard shortcut is used. The following common CSS selectors are
  155. intended to be used for adding context menu items and keyboard shortcuts.
  156. **CSS classes that target widgets and their children**
  157. - ``jp-Activity``: applied to elements in the main work area
  158. - ``jp-Cell``: applied to cells
  159. - ``jp-CodeCell``: applied to code cells
  160. - ``jp-CodeConsole``: applied to consoles
  161. - ``jp-CodeConsole-content``: applied to content panels in consoles
  162. - ``jp-CodeConsole-promptCell``: applied to active prompt cells in consoles
  163. - ``jp-DirListing-content``: applied to contents of file browser directory listings
  164. - ``jp-DirListing-item``: applied to items in file browser directory listings
  165. - ``jp-FileEditor``: applied to file editors
  166. - ``jp-ImageViewer``: applied to image viewers
  167. - ``jp-InputArea-editor``: applied to cell input area editors
  168. - ``jp-Notebook``: applied to notebooks
  169. - ``jp-SettingEditor``: applied to setting editors
  170. - ``jp-SideBar``: applied to sidebars
  171. - ``jp-Terminal``: applied to terminals
  172. **CSS classes that describe the state of a widget**
  173. - ``jp-mod-current``: applied to elements on the current document only
  174. - ``jp-mod-completer-enabled``: applied to ediors that can host a completer
  175. - ``jp-mod-commandMode``: applied to a notebook in command mode
  176. - ``jp-mod-editMode``: applied to a notebook in edit mode
  177. - ``jp-mod-has-primary-selection``: applied to editors that have a primary selection
  178. - ``jp-mod-in-leading-whitespace``: applied to editors that have a selection within the beginning whitespace of a line
  179. - ``jp-mod-tooltip``: applied to the body when a tooltip exists on the page
  180. **CSS selectors that target data attributes**
  181. - ``[data-jp-code-runner]``: applied to widgets that can run code
  182. - ``[data-jp-interaction-mode="terminal"]``: applied when a code console is in terminal mode
  183. - ``[data-jp-interaction-mode="notebook"]``: applied when a code console is in notebook mode
  184. - ``[data-jp-isdir]``: applied to describe whether file browser items are directories
  185. - ``[data-jp-undoer]``: applied to widgets that can undo
  186. - ``[data-type]``: applied to describe the type of element, such as "document-title", "submenu", "inline"