theming.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
  3. .. _theming:
  4. =======
  5. Theming
  6. =======
  7. Setting up your environment
  8. ===========================
  9. In order to theme Converse, you first need to follow the steps for :ref:`setup_dev_environment`, including :ref:`webserver`.
  10. Creating a custom theme
  11. =======================
  12. Converse can be themed via CSS custom properties (aka CSS variables) and it has
  13. some themes available in its source repository.
  14. A theme is a CSS file with a specific rule that defines the theme's CSS properties.
  15. The rule has a specific selector that must include (and determines) the theme name.
  16. Inside this CSS rule, various CSS variables are assigned values.
  17. The CSS variables mainly refer to the colors that comprise the theme.
  18. If you don't specify a value for a specific CSS variable, then the value from
  19. the ``classic`` theme is used, as defined in `classic.scss <https://github.com/conversejs/converse.js/tree/master/src/shared/styles/themes/classic.scss>`_.
  20. The native theme files can be found in `shared/styles/themes <https://github.com/conversejs/converse.js/tree/master/src/shared/styles/themes>`_.
  21. Note, the Converse theme files have a ``.scss`` extension because they are compiled
  22. by the Sass compiler into normal CSS files. It's however not necessary to use
  23. Sass, basic CSS files will also suffice.
  24. The theme that Converse uses can be set via the :ref:`theme` configuration
  25. setting (and the :ref:`dark_theme` configuration setting for dark mode).
  26. How are themes applied?
  27. -----------------------
  28. When you set a value for the :ref:`theme` configuration setting, Converse will add
  29. a class ``theme-${api.settings.get('theme')}`` on the ``converse-root`` DOM
  30. element.
  31. So, for example, if you set the ``theme`` setting to ``"dracula"``, then the
  32. ``converse-root`` element will get the class ``theme-dracula``.
  33. .. code-block:: javascript
  34. converse.initialize({ theme: "dracula" });
  35. .. code-block:: html
  36. <converse-root class="conversejs theme-dracula"></converse-root>
  37. The apply a theme, there then needs to be a CSS rule with a selector that matches the
  38. ``theme-dracula`` class on the ``converse-root`` element.
  39. If you take a look at the theme file `dracula.scss <https://github.com/conversejs/converse.js/tree/master/src/shared/styles/themes/dracula.scss>`_
  40. you'll see that it defines a CSS rule with the selector
  41. ``.conversejs.theme-dracula``.
  42. This selector matches any DOM element with both the classes ``.conversejs`` and
  43. ``.theme-dracula``. The ``converse-root`` element will already have the class
  44. ``.conversejs`` and it will have the class ``.theme-dracula`` if the ``theme``
  45. (or ``dark_theme`` in dark mode) configuration setting is set to ``"dracula"``.
  46. This is how themes are applied, by defining a CSS selector that matches the
  47. class ``.theme-${name}`` (where ``name`` is a variable containing the name of
  48. the theme), and then setting the ``theme`` (and/or ``dark_theme``) configuration
  49. setting.
  50. To create your own theme, you can create a similar CSS rule that matches
  51. your theme's name and then you set the ``theme`` configuration setting to that
  52. name. This CSS rule can be in any CSS file that is loaded in your website, or
  53. you can even put it in the DOM as an inline style.
  54. Modifying the CSS
  55. =================
  56. To create a new theme with different colors, it should be enough to create a
  57. theme file that sets the various CSS variables (as described above).
  58. For other CSS-related changes, you can make a specific
  59. CSS rule with that matches the element you want to change.
  60. Sometimes it might however be neccessary to modify the core CSS files from
  61. Converse, for example if you're developing new features or fixing styling bugs.
  62. The CSS files are generated from `Sass <http://sass-lang.com>`_ files that end in ``.scss`` and
  63. which are distributed throughout the source code.
  64. The CSS that is relevant to a particular plugin
  65. is usually inside the ``./styles`` directory inside the relevant plugin directory.
  66. For example: `src/plugins/controlbox/styles <https://github.com/conversejs/converse.js/tree/master/src/plugins/controlbox/styles>`_.
  67. If you're running ``make watch``, then the CSS will automatically be
  68. regenerated when you've changed any of the ``.scss``.
  69. You can also manually generate the CSS::
  70. make css
  71. Modifying the HTML templates of Converse
  72. ========================================
  73. Converse uses `lit-html <https://lit.dev/docs/libraries/standalone-templates/>`_ as HTML
  74. templating library, and the HTML source code is contained in JavaScript ``.js``
  75. files in various ``./template`` directories in the source code.
  76. Some top-level templates are also in the ``./src/templates`` directory, but
  77. the templates that are relevant to a specific plugin should be inside that plugin's subdirectory.
  78. For example: `src/plugins/chatview/templates <https://github.com/conversejs/converse.js/tree/master/src/plugins/chatview/templates>`_.
  79. You can modify HTML markup that Converse generates by modifying these files.
  80. Use webpack aliases to modify templates without changing the original files
  81. ---------------------------------------------------------------------------
  82. Generally what I do when creating a modified version of Converse for a project
  83. or customer, is that I create a new JavaScript package with its own
  84. ``package.json`` and I then add ``converse.js`` as a dependency (e.g. via ``npm
  85. install --save converse.js``) to the ``package.json``.
  86. Then I add a Webpack configuration and use `webpack aliases <https://webpack.js.org/configuration/resolve/#resolvealias>`_
  87. to resolve template paths to my own modified files.
  88. For example, in the webpack configuration snippet below, I add two aliases, so
  89. that the ``message-body.js`` and ``message.js`` templates can be replaced with
  90. two of my own custom templates.
  91. .. code-block:: javascript
  92. resolve: {
  93. extensions: ['.js'],
  94. alias: {
  95. './message-body.js': path.resolve(__dirname, 'path/to/my/custom/message-body.js'),
  96. './templates/message.js': path.resolve(__dirname, 'path/to/my/custom/chat_message.js'),
  97. }
  98. }