-
-
Notifications
You must be signed in to change notification settings - Fork 10k
feat(theme): allow custom icons in DocCard component #11596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75e9502
9428b8a
aa5be88
257868b
e5b5962
1f53416
85e693e
41e0283
84ff66d
65bfdac
5320efc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,6 +199,7 @@ export type PropSidebarItemCategory = Expand< | |
| export type PropSidebarItemLink = SidebarItemLink & { | ||
| docId?: string; | ||
| unlisted?: boolean; | ||
| icon?: string; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type describes above all the props that Docusaurus passes to the components, after transforming sidebar items to component props. If you add it here just to enable a new component prop, this is quite odd because we still can't add an We actually want to allow users to provide custom icons through sidebar files, and need to design that feature properly: #7844 |
||
| }; | ||
|
|
||
| export type PropSidebarItemHtml = SidebarItemHtml; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,8 +113,14 @@ function CardCategory({item}: {item: PropSidebarItemCategory}): ReactNode { | |
| } | ||
|
|
||
| function CardLink({item}: {item: PropSidebarItemLink}): ReactNode { | ||
| const icon = isInternalUrl(item.href) ? '📄️' : '🔗'; | ||
| const defaultIcon = isInternalUrl(item.href) ? '📄' : '🔗'; | ||
|
|
||
| const customIcon = | ||
| typeof item.icon === 'string' && item.icon.length <= 2 ? item.icon : null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why Is this an unreliable heuristic to check if the icon is an emoji? |
||
|
|
||
| const icon = customIcon || defaultIcon; | ||
| const doc = useDocById(item.docId ?? undefined); | ||
|
|
||
| return ( | ||
| <CardLayout | ||
| className={item.className} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
| import png from '@site/static/img/docusaurus.png'; | ||
| import svgImg from '@site/static/img/docusaurus.svg'; | ||
| import jpg from '@site/static/img/endi.jpg'; | ||
|
|
||
| const items = [ | ||
| { | ||
| label: 'Custom Icon', | ||
| description: 'Custom Icon', | ||
| href: '/docs/installation', | ||
| icon: '📖', | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Custom Icon', | ||
| description: 'Custom Icon', | ||
| href: '/docs/configuration', | ||
| icon: '🖥', | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Custom Icon (external link)', | ||
| description: 'Custom Icon (external link)', | ||
| href: 'https://react.dev/', | ||
| icon: '🛠', | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Default Icon (internal link)', | ||
| description: 'Default Icon (internal link)', | ||
| href: '/docs/installation', | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Default Icon (internal link)', | ||
| description: 'Default Icon (internal link)', | ||
| href: '/docs/configuration', | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Default Icon (external link)', | ||
| description: 'Default Icon (external link)', | ||
| href: 'https://react.dev/', | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Custom Image', | ||
| description: 'It does not break with PNG', | ||
| href: 'https://react.dev/', | ||
| icon: png, | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Custom Image', | ||
| description: 'It does not break with JPG', | ||
| href: '/docs/installation', | ||
| icon: jpg, | ||
| type: 'link', | ||
| }, | ||
| { | ||
| label: 'Custom Image', | ||
| description: 'It does not break with SVG', | ||
| href: 'https://react.dev/', | ||
| icon: svgImg, | ||
| type: 'link', | ||
| }, | ||
| ]; | ||
|
|
||
| export default items; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # DocCards Test | ||
|
|
||
| import DocCard from '@theme/DocCard'; | ||
| import DocCardList from '@theme/DocCardList'; | ||
|
|
||
| import items from './doc-cards-items.js'; | ||
|
|
||
| <DocCardList items={items} /> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this works for you, but is not really the most idiomatic usage of sidebar items |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, we can customize the icon of docs/links, but not categories?