diff --git a/README.md b/README.md index 18d37e8..c80d0f6 100644 --- a/README.md +++ b/README.md @@ -249,3 +249,57 @@ It will produce `Return to Home` `Return back` + +## Understand Astro's scoped CSS + +By default, everything is scoped to the current custom component. This mean you can't style a custom child component. +If you do want to style a custom child component, there are two ways: + +- Using :global() +- Passing a class to the child component + +Passing a class will enable two things on one element of the child component (typically the root element): + +- It will now have the class +- It will also have the parent component CID (Astro's Scope-CSS ID) + +### Example + +We have two components: + +```html +// Component A + +
+

This is Component A

+ +
+ +// Component B + +
+

This is Component B

+
+``` + +Now if Component A pass a class "test" to Component B: + +```html +
+

This is Component A

+ +
+``` + +The resulting Component B will be: + +``` +
+

This is Component B

+
+``` + +Things to consider: + +- The root element of Component B (the one where we applied the CID) is now in the scope of Component A's CSS. +- The opposite isn't true: Component B's scoped CSS cannot affect Component A. diff --git a/src/components/AppLayout/components/Topbar/Topbar.astro b/src/components/AppLayout/components/Topbar/Topbar.astro index 4373b26..13c0c38 100644 --- a/src/components/AppLayout/components/Topbar/Topbar.astro +++ b/src/components/AppLayout/components/Topbar/Topbar.astro @@ -80,10 +80,6 @@ const { t, getLocalizedUrl } = await getI18n(Astro.locals.currentLocale); padding: 3px; /* This way focus outline isn't cropped by overflow-x */ gap: 8px; margin-left: -0.8em; - - & > :global(*) { - flex-shrink: 0; - } } & > #toolbar { @@ -113,16 +109,16 @@ const { t, getLocalizedUrl } = await getI18n(Astro.locals.currentLocale); display: flex; gap: 12px; - & > :global(.m-only) { + & > .m-only { display: none; } @media (max-width: 40rem) { - & > :global(.m-only) { + & > .m-only { display: flex; } - & > :global(.m-not) { + & > .m-not { display: none; } } diff --git a/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro b/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro index 37d6077..10f7ee2 100644 --- a/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro +++ b/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro @@ -10,7 +10,7 @@ interface Props { class?: string | undefined; } -const { withTitle, class: className } = Astro.props; +const { withTitle, ...otherProps } = Astro.props; const { t } = await getI18n(Astro.locals.currentLocale); const { currentCurrency } = Astro.locals; @@ -18,7 +18,7 @@ const { currentCurrency } = Astro.locals; {/* ------------------------------------------- HTML ------------------------------------------- */} - +
{ cache.currencies.map((id) => ( diff --git a/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro b/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro index 73239b4..f3008b5 100644 --- a/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro +++ b/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro @@ -10,7 +10,7 @@ interface Props { class?: string | undefined; } -const { withTitle, class: className } = Astro.props; +const { withTitle, ...otherProps } = Astro.props; const { currentLocale } = Astro.locals; const { t } = await getI18n(currentLocale); @@ -18,7 +18,7 @@ const { t } = await getI18n(currentLocale); {/* ------------------------------------------- HTML ------------------------------------------- */} - +
{ cache.locales.map(({ id }) => ( diff --git a/src/components/AudioPlayer.astro b/src/components/AudioPlayer.astro index 1dbd55f..e5af614 100644 --- a/src/components/AudioPlayer.astro +++ b/src/components/AudioPlayer.astro @@ -3,16 +3,18 @@ import type { EndpointAudio } from "src/shared/payload/payload-sdk"; interface Props { audio: EndpointAudio; + class?: string | undefined; } const { audio: { url, mimeType }, + ...otherProps } = Astro.props; --- {/* ------------------------------------------- HTML ------------------------------------------- */} -