Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
fix(ThemeSection): Make "name" prop optional, noissue
Browse files Browse the repository at this point in the history
  • Loading branch information
diondiondion committed Nov 17, 2021
1 parent 3cbf9a0 commit 8d912d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/ThemeSection/getThemeSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import {ConfigThemeSection, LocalThemeSection} from '../theme/types';

interface SectionGetterOptions {
theme: LocalThemeSection;
name: string;
name?: string;
withBackgroundImage?: boolean;
}

function getThemeSection({
theme,
name,
withBackgroundImage,
}: SectionGetterOptions): ConfigThemeSection {
}: SectionGetterOptions): ConfigThemeSection | undefined {
// If the passed in name isn't the name of a section,
// we treat it as a "color block"
const isThemeSection = Boolean(theme.sections?.[name]);
Expand Down Expand Up @@ -68,8 +68,9 @@ function getThemeSection({
// and we need to dynamically construct a readable section
const background = getColorBlock(name, theme);

// If this turns out not to be a valid colour, we give up
if (!isValidColor(background)) {
return localThemeSection;
return undefined;
}

const [contrastingText, contrastingShade] = isDark(background)
Expand Down
11 changes: 10 additions & 1 deletion src/ThemeSection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ThemeSectionProps {
* The key (name) of the theme section, as defined in `baseTheme.sections`,
* or a color block from e.g. `baseTheme.globals.colorBlocks`
*/
name: ThemeSectionName;
name?: ThemeSectionName;
/**
* Theme object to use as the base for all nested
* ThemeSections. Usually this prop only needs to be
Expand All @@ -34,11 +34,20 @@ function ThemeSection(props: ThemeSectionProps): React.ReactElement {

const constructLocalTheme = useCallback(
(parentTheme = baseTheme): LocalThemeSection => {
if (!name && !withBackgroundImage) {
return parentTheme;
}

const localThemeSection = getThemeSection({
theme: parentTheme,
name,
withBackgroundImage,
});

if (!localThemeSection) {
return parentTheme;
}

const parentThemeSectionName = parentTheme.currentThemeSectionName;

return {
Expand Down

0 comments on commit 8d912d2

Please sign in to comment.