サイドバーで折りたたみ可能なサブカテゴリ

これに関する投稿をいくつか見ましたが、決定的な答えはありませんでした。

Unreal Engine Discourse(Discourseの「Discover」ページで紹介されている)は完全にカスタムテーマを使用していますか、それともこれを実現できるコンポーネントはありますか?

また、サブカテゴリをクリックすると、サブカテゴリがトップレベルのリンクとして表示され、その下にすべてのトピックがリストされることに気づきました。ここで起こっていることは、「親カテゴリ」で「Subcategory List Style」を「Rows」に設定してこれを実現しているようです。

「いいね!」 1

これがおそらくコンポーネントだけでは解決できないもののようです。

しかし、私はAIの少しの助けを借りてこれを実現するJSとCSSを生成できました。興味がある方には喜んで共有します。

「いいね!」 3

これを見たと思いますが:

そしてこれも:

どうぞ、コードを共有してください。もちろん、このようなものの課題は、継続的なメンテナンスです。

「いいね!」 2

はい、彼らがやっていることの多くはカスタムのもののようです。

私のスクリプトは次のようになっています:

import { apiInitializer } from "discourse/lib/api";
import { ajax } from "discourse/lib/ajax";

export default apiInitializer("0.11.1", (api) = {
  ajax("/site.json").then((data) = {
    const categories = data.categories;

    // 親IDをキー、それに属する子カテゴリIDの配列を値とするマップを作成
    const childMap = {};
    categories.forEach((cat) = {
      if (cat.parent_category_id) {
        if (!childMap[cat.parent_category_id]) {
          childMap[cat.parent_category_id] = [];
        }
        childMap[cat.parent_category_id].push(cat.id);
      }
    });

    const collapseState = {}; // 親ごとの折りたたみ状態を追跡

    function applyCollapseState(parentId, childIds, collapsed) {
      childIds.forEach((childId) = {
        const childEl = document.querySelector(
          `.sidebar-section-link-wrapper[data-category-id="${childId}"]`
        );
        if (childEl) {
          childEl.classList.toggle("is-collapsed", collapsed);
          childEl.classList.add("is-subcategory");
        }
      });
    }

    function ensureToggleExists(parentId, childIds) {
      const parentEl = document.querySelector(
        `.sidebar-section-link-wrapper[data-category-id="${parentId}"]`
      );
      if (!parentEl || parentEl.classList.contains("has-toggle")) return;

      const toggle = document.createElement("span");
      toggle.innerText = collapseState[parentId] ? "▸" : "▾";
      toggle.className = "toggle-subcategories";
      toggle.style.cursor = "pointer";
      toggle.style.marginLeft = "0.5em";

      toggle.onclick = () = {
        const isNowCollapsed = !collapseState[parentId];
        applyCollapseState(parentId, childIds, isNowCollapsed);
        toggle.innerText = isNowCollapsed ? "▸" : "▾";
        collapseState[parentId] = isNowCollapsed;
      };

      parentEl.classList.add("has-toggle");
      const link = parentEl.querySelector(".sidebar-section-link");
      if (link) link.appendChild(toggle);
    }

    api.onPageChange(() = {
      Object.entries(childMap).forEach(([parentId, childIds]) = {
        // デフォルトの折りたたみ状態を設定
        if (collapseState[parentId] === undefined) {
          collapseState[parentId] = true;
        }

        applyCollapseState(parentId, childIds, collapseState[parentId]);
        ensureToggleExists(parentId, childIds);
      });

      const container = document.querySelector('[data-section-name="categories"]');
      if (!container) return;

      const observer = new MutationObserver(() = {
        Object.entries(childMap).forEach(([parentId, childIds]) = {
          applyCollapseState(parentId, childIds, collapseState[parentId]);
          ensureToggleExists(parentId, childIds); // トグルを復元
        });
      });

      observer.observe(container, {
        childList: true,
        subtree: true,
      });
    });
  });
});

これをテーマのJSパネルに入れただけです。関連するCSSは次の通りです:

.sidebar-section-link-wrapper.is-collapsed {
  display: none !important;
}

.sidebar-section-link-wrapper.is-subcategory {
  padding-left: 1.5em;
}

.toggle-subcategories {
    float: right;
    display: flex;
    width: 30px;
    align-items: center;
    font-size: .9em;
    line-height: 1;
    height: 30px;
}

十分に目的を達していますが、多くのサブカテゴリがある場合にはおすすめしません。