Skip to content
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

docs(MapboxMap): add storybook stories #312

Merged
merged 48 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
f4eac1f
docs(MapboxMap): add story for component
tatimblin Oct 6, 2022
ba42394
docs(MapboxMap): add storybook stories
tatimblin Oct 10, 2022
a865f05
Automated update to repo's documentation from github action
github-actions[bot] Oct 10, 2022
afca03c
chore(MapboxMap): add percy secret
tatimblin Oct 11, 2022
057e0b2
fix(MapboxMap): update custom mapPin story
tatimblin Oct 12, 2022
57b9c6b
chore: change reusable workflow branch
tatimblin Oct 12, 2022
544236f
refactor(MapboxMap): remove initialRenderRef
tatimblin Oct 12, 2022
b0e5303
Update preview.js
tmeyer2115 Oct 12, 2022
85f5939
move story data to separate file
tatimblin Oct 12, 2022
5a92634
update feature branch linting rule
tatimblin Oct 12, 2022
cb49a38
test config
tatimblin Oct 12, 2022
6b69f82
move secret
tatimblin Oct 12, 2022
a13c9e2
change hook
tatimblin Oct 12, 2022
371bcb7
refactor(MapboxMap): use callback hook
tatimblin Oct 12, 2022
b28f601
inputs
tatimblin Oct 12, 2022
b739c46
secrets
tatimblin Oct 12, 2022
31b21eb
update build script
tatimblin Oct 12, 2022
fdf0eeb
npx
tatimblin Oct 12, 2022
a8ed46d
staticDirs
tatimblin Oct 12, 2022
5a2d3f9
env passthrough
tatimblin Oct 12, 2022
e34ed93
remove auto import
tatimblin Oct 12, 2022
9730aa0
remove coordinate passthrough
tatimblin Oct 13, 2022
6ef2919
Automated update to repo's documentation from github action
github-actions[bot] Oct 13, 2022
8437fa8
update env var
tatimblin Oct 13, 2022
485926c
update env var
tatimblin Oct 13, 2022
1bc0565
rename var
tatimblin Oct 13, 2022
2963829
rerun
tatimblin Oct 13, 2022
fc72fc0
add storybook interaction
tatimblin Oct 13, 2022
1733b9b
check for popup content
tatimblin Oct 13, 2022
8b55e00
lint
tatimblin Oct 13, 2022
bf8a2c2
add interaction query to check map loaded
tatimblin Oct 14, 2022
3866537
add data attr on load
tatimblin Oct 14, 2022
543c001
add timeout to loaded check
tatimblin Oct 14, 2022
5be846a
reduce timeout
tatimblin Oct 14, 2022
5e8ca29
remove screen import
tatimblin Oct 14, 2022
81d6424
adjust timeout
tatimblin Oct 14, 2022
d1dde9a
adjust timeout
tatimblin Oct 14, 2022
d7ac312
reorder interactions
tatimblin Oct 14, 2022
e2efd0f
test
tatimblin Oct 14, 2022
8ec3c80
test
tatimblin Oct 14, 2022
3f8d586
adjust timeout
tatimblin Oct 14, 2022
5b30669
adjust timeout
tatimblin Oct 14, 2022
9b64550
add forced await
tatimblin Oct 17, 2022
d5acbab
lint
tatimblin Oct 17, 2022
3554ee0
add a lot of time
tatimblin Oct 17, 2022
955a539
remove forced delay
tatimblin Oct 17, 2022
86ff7bd
Fix Percy snapshots
nmanu1 Oct 18, 2022
2e46f46
Fix TS error and remove extra code
nmanu1 Oct 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './index.css';
import 'mapbox-gl/dist/mapbox-gl.css';
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
import { SearchCoreDecorator } from '../tests/__fixtures__/core/SearchCore';
import { runOnly } from './wcagConfig';

Expand Down Expand Up @@ -35,7 +36,8 @@ export const parameters = {
'SpellCheck',
'ResultsCount',
'LocationBias',
'Dropdown'
'Dropdown',
'MapboxMap',
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
]
}
}
Expand Down
7 changes: 6 additions & 1 deletion .storybook/tailwind.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ module.exports = {
...tailwindConfig,
content: [
'./src/**/*.{ts,tsx}'
]
],
safelist: [
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
{
pattern: /^(.*?)/,
},
],
};
1 change: 1 addition & 0 deletions sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_MAPBOX_API_KEY=[[Your Mapbox API Key]]
oshi97 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions src/components/MapboxMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ReactDOM from 'react-dom';
*
* @public
*/
export type PinComponent<T> = (props: { result?: Result<T>, index?: number }) => JSX.Element;
export type PinComponent<T> = (props: { result: Result<T>, index: number, mapbox: Map }) => JSX.Element;

/**
* A function use to derive a result's coordinate.
Expand Down Expand Up @@ -132,7 +132,7 @@ export function MapboxMap<T>({
const el = document.createElement('div');
const markerOptions: MarkerOptions = {};
if (PinComponent) {
ReactDOM.render(<PinComponent result={result} index={i} />, el);
ReactDOM.render(<PinComponent result={result} index={i} mapbox={mapbox} />, el);
markerOptions.element = el;
}
const marker = new Marker(markerOptions)
Expand Down
47 changes: 43 additions & 4 deletions test-site/src/components/MapPin.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
import { PinComponent } from '@yext/search-ui-react';
import { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { Map, Popup, LngLatLike } from 'mapbox-gl';
import { PinComponent, Coordinate } from '@yext/search-ui-react';
import { Result } from '@yext/search-headless-react';
import { Location } from '../pages/LocationsPage';
import { MapPopup } from './MapPopup';

interface MapPinProps {
result: Result<Location>
index: number
mapbox: Map
}

const transformToMapboxCoord = (coordinate: Coordinate): LngLatLike => ({
lng: coordinate.longitude,
lat: coordinate.latitude,
});

export const MapPin: PinComponent<Location> = (props: MapPinProps) => {
const popupRef = useRef(new Popup({ offset: 15 }));
const initialRender = useRef(true);
yen-tt marked this conversation as resolved.
Show resolved Hide resolved
const [active, setActive] = useState(false);
oshi97 marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
oshi97 marked this conversation as resolved.
Show resolved Hide resolved
if (!initialRender.current && active) {
const popup = document.createElement('div');
ReactDOM.render(<MapPopup entity={props.result} />, popup);
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
popupRef.current
.setLngLat(coordinate)
.setDOMContent(popup)
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
.addTo(props.mapbox);
}
initialRender.current = false;
}, [active]);
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
tatimblin marked this conversation as resolved.
Show resolved Hide resolved

const yextCoordinate: Location["yextDisplayCoordinate"] = props.result.rawData.yextDisplayCoordinate;
if (!yextCoordinate) return <></>;
const coordinate = transformToMapboxCoord(yextCoordinate);

export const MapPin: PinComponent<Location> = () => {
return (
<div className='relative z-10'>
<button
className='relative z-10'
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
onClick={() => setActive(!active)}
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
>
<svg
className='z-40 fill-slate-800 hover:fill-slate-500 stroke-2 stroke-white'
width="33" height="42"
Expand All @@ -13,6 +52,6 @@ export const MapPin: PinComponent<Location> = () => {
>
<path d="M30 15.0882C30 23.4212 23.3333 30.7353 15 38C7.22222 31.2941 0 23.4212 0 15.0882C0 6.75523 6.71573 0 15 0C23.2843 0 30 6.75523 30 15.0882Z" />
</svg>
</div>
</button>
);
};
14 changes: 14 additions & 0 deletions test-site/src/components/MapPopup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Result } from '@yext/search-headless-react';
import { Location } from '../pages/LocationsPage';

interface MapPopupProps {
entity: Result<Location>
}

export const MapPopup = ({ entity }: MapPopupProps) => {
return (
<div>
{entity.name}
</div>
);
}
6 changes: 2 additions & 4 deletions test-site/src/pages/LocationsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import {
Pagination,
MapboxMapProps,
OnDragHandler,
Coordinate
} from '@yext/search-ui-react';
import { LngLat, LngLatBounds } from 'mapbox-gl';
import { useCallback, useLayoutEffect } from 'react';
import { MapPin } from '../components/MapPin';

export interface Location {
yextDisplayCoordinate: {
latitude: number,
longitude: number
} | undefined
yextDisplayCoordinate: Coordinate | undefined
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
}

const mapboxOptions: MapboxMapProps<Location>['mapboxOptions'] = {
Expand Down
88 changes: 88 additions & 0 deletions tests/components/MapboxMap.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { useCallback } from 'react';
import { ComponentMeta } from '@storybook/react';
import { SearchHeadlessContext, Source } from '@yext/search-headless-react';

import { generateMockedHeadless } from '../__fixtures__/search-headless';
import { MapboxMap, MapboxMapProps } from '../../src/components/MapboxMap';
import { MapPin } from '../../test-site/src/components/MapPin';

const meta: ComponentMeta<typeof MapboxMap<Location>> = {
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
title: 'MapboxMap',
component: MapboxMap,
argTypes: {
mapboxAccessToken: {
defaultValue: process.env.REACT_APP_MAPBOX_API_KEY,
control: {
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
type: null,
}
}
},
parameters: { layout: 'fullscreen' },
decorators: [(Story) => (<div className="h-screen"><Story /></div>)]
};
export default meta;

const mockedHeadlessState = {
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
vertical: {
results: [
{
name: 'title1',
rawData: {
name: 'title1',
description: 'text1',
yextDisplayCoordinate: {
latitude: 40.741611,
longitude: -74.005371,
}
},
source: Source.KnowledgeManager,
id: 'id1'
}
],
resultsCount: 1,
limit: 1
}
};

// Deep copy state to make an alternative with multiple locations
let mockedHeadlessStateMultiple = JSON.parse(JSON.stringify(mockedHeadlessState));
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
mockedHeadlessStateMultiple.vertical.results.push(
{
name: 'title2',
rawData: {
name: 'title2',
description: 'text2',
yextDisplayCoordinate: {
latitude: 40.641611,
longitude: -74.005371,
}
},
source: Source.KnowledgeManager,
id: 'id2'
}
);

export const Primary = (args: MapboxMapProps<Location>) => {
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
return (
<SearchHeadlessContext.Provider value={generateMockedHeadless(mockedHeadlessState)}>
<MapboxMap {...args} />
</SearchHeadlessContext.Provider>
);
};

export const MultiplePins = (args: MapboxMapProps<Location>) => {
return (
<SearchHeadlessContext.Provider value={generateMockedHeadless(mockedHeadlessStateMultiple)}>
<MapboxMap {...args} />
</SearchHeadlessContext.Provider>
);
};

export const CustomPin = (args: MapboxMapProps<Location>) => {
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
return (
<SearchHeadlessContext.Provider value={generateMockedHeadless(mockedHeadlessState)}>
<MapboxMap PinComponent={MapPin} {...args} />
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
</SearchHeadlessContext.Provider>
);
};