Skip to content

Commit 41ef9da

Browse files
cerkiewnyJosh-Cena
andauthored
feat(theme-classic): allow specifying width/height in logo (#5770)
* feat: changed the logo properties to allow width/height specification * fixup! feat: changed the logo properties to allow width/height specification * fixup! feat: changed the logo properties to allow width/height specification * Rework: add fields to logo object * Fix * More fixes * Wrong width! * No need for optional chaining * Doc writeup Co-authored-by: Josh-Cena <[email protected]>
1 parent 895c848 commit 41ef9da

File tree

8 files changed

+66
-4
lines changed

8 files changed

+66
-4
lines changed

packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,27 @@ describe('themeConfig', () => {
345345
});
346346
});
347347

348+
test('should allow width and height specification for logo ', () => {
349+
const altTagConfig = {
350+
navbar: {
351+
logo: {
352+
alt: '',
353+
src: '/arbitrary-logo.png',
354+
srcDark: '/arbitrary-dark-logo.png',
355+
width: '20px',
356+
height: '20%',
357+
},
358+
},
359+
};
360+
expect(testValidateThemeConfig(altTagConfig)).toEqual({
361+
...DEFAULT_CONFIG,
362+
navbar: {
363+
...DEFAULT_CONFIG.navbar,
364+
...altTagConfig.navbar,
365+
},
366+
});
367+
});
368+
348369
test('should accept valid prism config', () => {
349370
const prismConfig = {
350371
prism: {

packages/docusaurus-theme-classic/src/theme/Footer/index.tsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ function FooterLink({
5252
const FooterLogo = ({
5353
sources,
5454
alt,
55-
}: Pick<ThemedImageProps, 'sources' | 'alt'>) => (
56-
<ThemedImage className="footer__logo" alt={alt} sources={sources} />
55+
width,
56+
height,
57+
}: Pick<ThemedImageProps, 'sources' | 'alt' | 'width' | 'height'>) => (
58+
<ThemedImage
59+
className="footer__logo"
60+
alt={alt}
61+
sources={sources}
62+
width={width}
63+
height={height}
64+
/>
5765
);
5866

5967
function Footer(): JSX.Element | null {
@@ -115,7 +123,12 @@ function Footer(): JSX.Element | null {
115123
<div className="margin-bottom--sm">
116124
{logo.href ? (
117125
<Link href={logo.href} className={styles.footerLogoLink}>
118-
<FooterLogo alt={logo.alt} sources={sources} />
126+
<FooterLogo
127+
alt={logo.alt}
128+
sources={sources}
129+
width={logo.width}
130+
height={logo.height}
131+
/>
119132
</Link>
120133
) : (
121134
<FooterLogo alt={logo.alt} sources={sources} />

packages/docusaurus-theme-classic/src/theme/Logo/index.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ const Logo = (props: Props): JSX.Element => {
2929
dark: useBaseUrl(logo.srcDark || logo.src),
3030
};
3131
const themedImage = (
32-
<ThemedImage sources={sources} alt={logo.alt || navbarTitle || title} />
32+
<ThemedImage
33+
sources={sources}
34+
height={logo.height}
35+
width={logo.width}
36+
alt={logo.alt || navbarTitle || title}
37+
/>
3338
);
3439

3540
return (

packages/docusaurus-theme-classic/src/validateThemeConfig.ts

+5
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ const ThemeConfigSchema = Joi.object({
290290
alt: Joi.string().allow(''),
291291
src: Joi.string().required(),
292292
srcDark: Joi.string(),
293+
width: Joi.alternatives().try(Joi.string(), Joi.number()),
294+
height: Joi.alternatives().try(Joi.string(), Joi.number()),
293295
href: Joi.string(),
294296
target: Joi.string(),
295297
}),
@@ -300,6 +302,9 @@ const ThemeConfigSchema = Joi.object({
300302
alt: Joi.string().allow(''),
301303
src: Joi.string(),
302304
srcDark: Joi.string(),
305+
// TODO infer this from reading the image
306+
width: Joi.alternatives().try(Joi.string(), Joi.number()),
307+
height: Joi.alternatives().try(Joi.string(), Joi.number()),
303308
href: Joi.string(),
304309
}),
305310
copyright: Joi.string(),

packages/docusaurus-theme-common/src/utils/useThemeConfig.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export type NavbarItem = {
2222
export type NavbarLogo = {
2323
src: string;
2424
srcDark?: string;
25+
width?: string | number;
26+
height?: string | number;
2527
href?: string;
2628
target?: string;
2729
alt?: string;
@@ -80,6 +82,8 @@ export type Footer = {
8082
alt?: string;
8183
src?: string;
8284
srcDark?: string;
85+
width?: string | number;
86+
height?: string | number;
8387
href?: string;
8488
};
8589
copyright?: string;

website/docs/api/docusaurus.config.js.md

+4
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ module.exports = {
264264
logo: {
265265
alt: 'Site Logo',
266266
src: 'img/logo.svg',
267+
width: 32,
268+
height: 32,
267269
},
268270
items: [
269271
{
@@ -292,6 +294,8 @@ module.exports = {
292294
logo: {
293295
alt: 'Facebook Open Source Logo',
294296
src: 'https://docusaurus.io/img/oss_logo.png',
297+
width: 160,
298+
height: 51,
295299
},
296300
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, // You can also put own HTML here
297301
},

website/docs/api/themes/theme-configuration.md

+6
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ Accepted fields:
187187
| `src` | `string` | **Required** | URL to the logo image. Base URL is appended by default. |
188188
| `srcDark` | `string` | `logo.src` | An alternative image URL to use in dark mode. |
189189
| `href` | `string` | `siteConfig.baseUrl` | Link to navigate to when the logo is clicked. |
190+
| `width` | <code>string \| number</code> | `undefined` | Specifies the `width` attribute. |
191+
| `height` | <code>string \| number</code> | `undefined` | Specifies the `height` attribute. |
190192
| `target` | `string` | Calculated based on `href` (external links will open in a new tab, all others in the current one). | The `target` attribute of the link; controls whether the link is opened in a new tab, the current one, or otherwise. |
191193

192194
</small>
@@ -205,6 +207,8 @@ module.exports = {
205207
srcDark: 'img/logo_dark.svg',
206208
href: 'https://docusaurus.io/',
207209
target: '_self',
210+
width: 32,
211+
height: 32,
208212
},
209213
// highlight-end
210214
},
@@ -679,6 +683,8 @@ module.exports = {
679683
alt: 'Facebook Open Source Logo',
680684
src: 'img/oss_logo.png',
681685
href: 'https://opensource.facebook.com',
686+
width: 160,
687+
height: 51,
682688
},
683689
copyright: `Copyright © ${new Date().getFullYear()} My Project, Inc. Built with Docusaurus.`,
684690
},

website/docusaurus.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ const config = {
334334
alt: 'Docusaurus Logo',
335335
src: 'img/docusaurus.svg',
336336
srcDark: 'img/docusaurus_keytar.svg',
337+
width: 32,
338+
height: 32,
337339
},
338340
items: [
339341
{
@@ -487,6 +489,8 @@ const config = {
487489
logo: {
488490
alt: 'Facebook Open Source Logo',
489491
src: 'img/oss_logo.png',
492+
width: 160,
493+
height: 51,
490494
href: 'https://opensource.facebook.com',
491495
},
492496
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc. Built with Docusaurus.`,

0 commit comments

Comments
 (0)