Skip to content

Commit

Permalink
refactor(examples) use globalThis instead of passing props
Browse files Browse the repository at this point in the history
  • Loading branch information
mrMetalWood committed Jan 18, 2024
1 parent c98a8b3 commit 04088f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
6 changes: 2 additions & 4 deletions examples/basic-map/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
<div id="app"></div>
<script type="module">
import '@vis.gl/react-google-maps/examples.css';
import {GOOGLE_MAPS_API_KEY} from '@vis.gl/react-google-maps/examples.js';
import '@vis.gl/react-google-maps/examples.js';
import {renderToDom} from './src/app';

renderToDom(document.querySelector('#app'), {
apiKey: GOOGLE_MAPS_API_KEY
});
renderToDom(document.querySelector('#app'));
</script>
</body>
</html>
15 changes: 6 additions & 9 deletions examples/basic-map/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import {createRoot} from 'react-dom/client';
import {APIProvider, Map} from '@vis.gl/react-google-maps';
import ControlPanel from './control-panel';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;
const API_KEY =
globalThis.GOOGLE_MAPS_API_KEY ?? (process.env.GOOGLE_MAPS_API_KEY as string);

interface Props {
apiKey?: string;
}

const App = (props: Props) => (
<APIProvider apiKey={props.apiKey ?? API_KEY}>
const App = () => (
<APIProvider apiKey={API_KEY}>
<Map
zoom={3}
center={{lat: 22.54992, lng: 0}}
Expand All @@ -23,12 +20,12 @@ const App = (props: Props) => (
);
export default App;

export function renderToDom(container: HTMLElement, props: Props) {
export function renderToDom(container: HTMLElement) {
const root = createRoot(container);

root.render(
<React.StrictMode>
<App {...props} />
<App />
</React.StrictMode>
);
}
5 changes: 5 additions & 0 deletions examples/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare global {
// const or let does not work in this case, it has to be var
// eslint-disable-next-line no-var
var GOOGLE_MAPS_API_KEY: string | undefined;
}
4 changes: 1 addition & 3 deletions website/static/scripts/examples.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// For the codesandbox examples, the 'process.env.GOOGLE_MAPS_API_KEY'
// gets replaced in the deploy task in the github action
// to contain the valid key for the examples
export default {
GOOGLE_MAPS_API_KEY: process.env.GOOGLE_MAPS_API_KEY
};
globalThis.GOOGLE_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY;

0 comments on commit 04088f4

Please sign in to comment.