Skip to content

Commit

Permalink
fix: use of useAppState outside preact context
Browse files Browse the repository at this point in the history
  • Loading branch information
DanWebb committed Jul 12, 2024
1 parent 40f00ba commit 2f61495
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
11 changes: 6 additions & 5 deletions src/components/canvas/MapSvg/MapSvg.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ locator-map-svg {
height: 100%;
width: 100%;

&::part(image),
&::part(content) {
img,
locator-map-svg-content {
grid-column: 1;
grid-row: 1;
}

&::part(image) {
img {
height: 100%;
object-fit: cover;
width: 100%;
}

&::part(content) {
locator-map-svg-content {
box-sizing: border-box;
display: block;
margin: 0 auto;
padding: var(--diamond-spacing);
width: min(100%, calc(var(--wrap-max-width) - var(--diamond-spacing-lg)));
}

@container (width > 768px) {
&::part(content) {
locator-map-svg-content {
place-self: end;
width: calc(var(--wrap-max-width) - var(--diamond-spacing-xl));
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/canvas/MapSvg/MapSvg.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, StoryObj } from '@storybook/preact';

import './MapSvg';
import MapSvgComponent from './MapSvg';

const meta: Meta = {
title: 'Components/Canvas/MapSvg',
Expand All @@ -9,5 +9,5 @@ const meta: Meta = {
export default meta;

export const MapSvg: StoryObj = {
render: () => <locator-map-svg>Applies svg map background</locator-map-svg>,
render: () => <MapSvgComponent>Applies svg map background</MapSvgComponent>,
};
17 changes: 9 additions & 8 deletions src/components/canvas/MapSvg/MapSvg.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { ComponentChildren } from 'preact';
import register from 'preact-custom-element';

import { useAppState } from '@/lib/AppState';
import { CustomElement } from '@/types/customElement';

export default function MapSvg({
children,
}: Readonly<{ children: ComponentChildren }>) {
}: Readonly<{ children?: ComponentChildren }>) {
const { publicPath } = useAppState();
const imgSrc = `${publicPath}images/map.svg`;

return (
<>
<img part="image" src={`${publicPath}images/map.svg`} alt="" />
<div part="content">{children}</div>
</>
<locator-map-svg>
<img src={imgSrc} alt="" />
{children && (
<locator-map-svg-content>{children}</locator-map-svg-content>
)}
</locator-map-svg>
);
}

register(MapSvg, 'locator-map-svg', [], { shadow: true });

declare module 'react' {
namespace JSX {
interface IntrinsicElements {
'locator-map-svg': CustomElement;
'locator-map-svg-content': CustomElement;
}
}
}
8 changes: 2 additions & 6 deletions src/pages/[postcode]/places/place/place.layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import '@/components/composition/Wrap/Wrap';
import '@/components/content/HeaderTitle/HeaderTitle';
import '@/components/content/Icon/Icon';
import '@/components/control/NavBar/NavBar';
import '@/components/canvas/MapSvg/MapSvg';
import MapSvg from '@/components/canvas/MapSvg/MapSvg';
import PlacesMap from '@/components/control/PlacesMap/PlacesMap';
import directions from '@/lib/directions';
import useAnalytics from '@/lib/useAnalytics';
Expand Down Expand Up @@ -160,11 +160,7 @@ export default function PlaceLayout({
<Suspense fallback={null}>
<Await resolve={locationPromise}>
{(location) =>
location?.latitude ? (
<PlaceMap location={location} />
) : (
<locator-map-svg></locator-map-svg>
)
location?.latitude ? <PlaceMap location={location} /> : <MapSvg />
}
</Await>
</Suspense>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/[postcode]/postcode.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '@etchteam/diamond-ui/composition/Enter/Enter';
import '@etchteam/diamond-ui/control/Button/Button';

import '@/components/canvas/ContextHeader/ContextHeader';
import '@/components/canvas/MapSvg/MapSvg';
import MapSvg from '@/components/canvas/MapSvg/MapSvg';
import '@/components/canvas/IconCircle/IconCircle';
import '@/components/canvas/Loading/Loading';
import '@/components/canvas/Hero/Hero';
Expand Down Expand Up @@ -41,14 +41,14 @@ function MapErrorFallback({ postcode }: { readonly postcode: string }) {
const { t } = useTranslation();

return (
<locator-map-svg>
<MapSvg>
<diamond-button width="full-width">
<Link to={`/${postcode}/places/map`}>
{t('postcode.exploreTheMap')}
<locator-icon icon="map" color="primary"></locator-icon>
</Link>
</diamond-button>
</locator-map-svg>
</MapSvg>
);
}

Expand Down

0 comments on commit 2f61495

Please sign in to comment.