Material UI: deck gl covers grid - reactjs

I am working with React and Material-UI. I want to divide the code into two columns. In one of this, I want to render a map using deck.gl.
Apparently, deck.gl is rendered fullscreen and it covers all the screen. I even tried to set the attributes width={80} and height={80} to the DeckGL component but it is still covering the rest of the page.
App.js
import React, { Component } from 'react';
import './App.css';
import Grid from '#material-ui/core/Grid';
import Map from './Map.js';
class App extends Component {
render() {
return (
<div >
<Grid container spacing={24}>
<Grid item xs={3}>
Column text
</Grid>
<Grid item xs={9}>
<Map/>
</Grid>
</Grid>
</div>
);
}
}
export default App;
Map.js
/// app.js
import React from 'react';
import DeckGL, {PolygonLayer} from 'deck.gl';
import {StaticMap} from 'react-map-gl';
// Set your mapbox access token here
const MAPBOX_ACCESS_TOKEN = 'pk.eyJ1IjoibWZvZ2xpbyIsImEiOiJjamt2N2Z2aWkwNXJxM3BxNXo4Mmt1a3MwIn0.IqS5iv1ZmLht4hm-N0cDYg';
// Initial viewport settings
const initialViewState = {
longitude: -87.630259,
latitude: 41.873400,
zoom: 13,
pitch: 35,
bearing: 0
};
// Data to be used by the LineLayer
const data = [{sourcePosition: [-122.41669, 37.7853], targetPosition: [-122.41669, 37.781]}];
class Map extends React.Component {
render() {
const layers = [
];
return (
<DeckGL
initialViewState={initialViewState}
controller={true}
layers={layers}
>
<StaticMap
mapboxApiAccessToken={MAPBOX_ACCESS_TOKEN}
/>
</DeckGL>
);
}
}
export default Map;

9Try this.
<Grid container spacing={24}>
<Grid item xs={3}>
Column text
</Grid>
<Grid item xs={3} lg={9}>
<Map/>
</Grid>

i had the same problem, the problem is that deck.gl by default is 100% width and 100% height and position absolute so it displays on top of your grid, what i did i manually overwrote deck.gl style rules in my css with '!important' rule or the second option you can put deck.gl in another div and give that div with height because deck uses 100% its parent height and width

I use react-container-dimensions to solve this problem.
<Grid key={value} item>
<Paper className={classes.paper}>
<ContainerDimensions>
<Map />
</ContainerDimensions>
</Paper>
</Grid

You just need to give height and width to the div and render that deckGL map into that div. It will show you accordingly. Because deck always follow its parents properties.

Related

Google maps doens't load in my react app (feat, GoogleMapReact)

I'm trying to make a react app that loads a google map.
but somehow the map doesn't show and not an error was reported in console logs....
import React from 'react'
import GoogleMapReact from 'google-map-react'
import {Paper, Typography, useMediaQuery} from '#material-ui/core'
import LocationOnOutlinedIcon from '#material-ui/icons/LocationOnOutlined'
import Rating from '#material-ui/lab'
import useStyles from './styles'
const Map = () => {
const classes = useStyles()
const isMobile = useMediaQuery('(min-width:600px)')
const coordinates = { lat:0, lng: 0}
return (
<div className={classes.mapContainer}>
<GoogleMapReact
bootstrapURLKeys={{ key: 'AIzaSyBrSAzFufdmJBVojpd7idemPVGp8HskFKY' }}
defaultCenter={coordinates}
center={coordinates}
defaultZoom={14}
margin={[50,50,50,50]}
>
</GoogleMapReact>
</div>
)
}
export default Map
ㄴ this is Map.js file
import Header from "./components/Header/Header";
import {List} from "./components/List/List";
import Map from "./components/Map/Map";
import {CssBaseline, Grid} from '#material-ui/core';
function App() {
return (
<div className="App">
<CssBaseline></CssBaseline>
<Header/>
<Grid container spacing={3} style={{ width: '100%'}}>
<Grid item xs={12} md={4}>
<List/>
</Grid>
<Grid item xs={12} md={8}>
<Map/>
</Grid>
</Grid>
</div>
);
}
export default App;
ㄴthis is App.js file
enter image description here
What's causing the error(if that is an error)?
j
How can I make the map show up in my app?

Module not found: Can't resolve '*.module.scss'

I'm new in react. when I'm enter a code using Grid component in material-ui.com it shows this error. Module not found: Can't resolve '.module.scss'* Do anyone know how to solve this.?
here is my code,
import { Grid, makeStyles, Paper } from '#material-ui/core';
import React from 'react';
import MC_MainH1 from '../components/MC_MainH1';
import MC_Para from '../components/MC_Para';
import image from './../img/4636927.jpg';
import './../components/styles/Para.css';
import classes from '*.module.scss';
function Features() {
return (
<Grid container>
<center>
<Grid item xs={5}>
<MC_MainH1 mainH1="Features"/>
</Grid>
</center>
<Grid item xs={12}>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
</Grid>
</Grid>
)
}
export default Features
I don't think you actually need to import classes. I had the same issue. Turns out the auto-complete simply added the import statement when I typed in classes.
To style material UI components,
`
const useStyles = makeStyles((theme)=>{
//Styling required
});
const classes = useStyles();
`

Which React Material-UI components add a spacing to my page (like a row class in Bootstrap)?

I've created a project using React and Material-UI for React.
Coming from a Bootstrap background, I've noticed none of these components come with any margin around their components.
In Bootstrap I can add spacing like this:
<div class="row">
<div class="col-xs-12">
...
</div>
</div>
But I've got no idea what component to use to create such spacing.
I'm currently using custom classes to create some sort of spacing, but it doesn't feel correct.
App.tsx:
<Container maxWidth="lg" className="container-padding">
...
</Container>
App.css:
.container-padding {
padding: 30px;
}
For example, add spacing between these elements with an existing component:
I'm open for suggestions.
There is a grid layout component in #material-ui similar to Bootstrap grid. Both are based on a 12-column grid.
The below example demonstrates it,
import Box from '#material-ui/core/Box';
import Grid from "#material-ui/core/Grid";
import Paper from "#material-ui/core/Paper";
return (
<Box m={4}>
<Grid container spacing={3}>
<Grid item xs={6}>
<Paper>xs=6</Paper>
</Grid>
<Grid item xs={6}>
<Paper>xs=6</Paper>
</Grid>
<Grid item xs={3}>
<Paper>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper>xs=3</Paper>
</Grid>
</Grid>
</Box>
<Box mx={3}>
Box 2 content
</Box>
<Box my={3}>
Box 3 content
</Box>
);
So to summarize,
m - all sides margin
mx - Horizontal spacing
my - Vertical spacing
I've used the "Lobotomized Owl" selector by Heydon Pickering: * + *.
I created a 'container' component Vertical.js:
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import { Box } from '#material-ui/core';
const useStyles = makeStyles((theme) => ({
vertical: {
'& > *+*': {
marginTop: '1.5rem',
},
},
}));
const Vertical = ({ children }) => {
const classes = useStyles();
return <Box className={classes.vertical}>{children}</Box>;
};
export default Vertical;
Then use it in any other components e.g. Example.js:
import React from 'react';
import Vertical from './Vertical';
const Example = () => {
return (
<Vertical>
<Component/>
<Component />
<Another />
<AnotherComponent />
</Vertical>
);
};
export default Example;

Custom layout in SimpleForm component on react-admin

I want to create a custom two-column-grid layout on my react-admin project on Edit and Show pages. I want to display selectboxes and the imageupload area on the left column, and the text inputs on the right column by using only one <SimpleForm>.
Simply like this
If I use a div or a <Card> component under <SimpleForm> and <EditController> components, I receive an error.
Warning: React does not recognize the `basePath` prop on a DOM element.
If you intentionally want it to appear in the DOM as a custom
attribute, spell it as lowercase `basepath` instead. If you
accidentally passed it from a parent component, remove it from the DOM
element.
Is there any way to create a layout without this error?
I solved it with creating another component with using divs, <Grid/> etc, and used that component in <SimpleForm> component.
import {withStyles} from '#material-ui/core/styles';
import React from 'react';
import {
EditController,
SimpleForm,
TextInput,
SelectInput,
Title,
} from 'react-admin';
import Grid from '#material-ui/core/Grid';
import Card from '#material-ui/core/Card';
import Poster from "../customField/Poster";
import {EditToolbar} from '../toolbar/CustomToolbar'
import {EditActions} from '../toolbar/CustomActions'
const editStyles = {
root: {display: 'flex', alignItems: 'flex-start', width: '100%'},
form: {flexGrow: 9},
};
class CardEdit extends React.Component {
constructor(props) {
super(props);
this.state = {
refresh: false
};
}
render() {
const FormDiv = withStyles(editStyles)(({children, classes, ...props}) => {
return (
<div className={classes.root}>
<div className={classes.form}>
<Grid container spacing={24}>
<Grid item xs={6}>
<TextInput source="name" fullWidth />
</Grid>
<Grid item xs={6}>
<TextInput source="card_id" fullWidth />
</Grid>
</Grid>
</div>
</div>
)
}
)
return (
<EditController {...this.props}>
{({resource, record, redirect, save, basePath, version}) => {
return (
<div>
<Title defaultTitle="sample"/>
<Card>
<div style={{ margin: '20px 20px 0 0' }}>
<EditActions
basePath={basePath}
resource={resource}
data={record}
hasShow
hasList
/>
</div>
{record && (
<SimpleForm
basePath={basePath}
redirect={redirect}
resource={resource}
record={record}
save={save}
version={version}
toolbar={<EditToolbar/>}
>
<FormDiv record={record} />
</SimpleForm>
)}
</Card>
</div>
)
}}
</EditController>
)
}
}
export default withStyles(editStyles)(CardEdit);
Actually, this could be done a little bit easier in case you don't need any custom styles and what not.
In order to get rid of the basePath error, just sanitize the props passed to the Material UI Grid Component:
const SanitizedGrid = ({basePath, ...props}) => {
return (
<Grid {...props} />
);
};
Then use it in place of a normal Grid:
export default props => (
<SimpleForm {...props}>
<SanitizedGrid container spacing={16}>
<Grid item xs>
<TextInput source="name" />
</Grid>
</SanitizedGrid>
</SimpleForm>
);
As another way, I've just worked out (thanks to Alexander's answer) a nice generic way to add any custom HTML content to a react-admin form:
import React, { Fragment } from 'react';
import { SimpleForm } from 'react-admin';
const CustomContent = ({ basePath, record, resource, children }) => (
<Fragment>
{children}
</Fragment>
);
export const MyForm = (props) => (
<SimpleForm>
<CustomContent>
<h3>Custom Content</h3>
<p>I can now add standard HTML to my react admin forms!</p>
</customContent>
</SimpleForm>
);
You get the basePath prop (which you probably don't want), but the record and resource props might be useful to your custom content (if you switch the code to use a render prop)

Add text inside leaflet rectangle

I'm using the following code to create a rectangle in the leaflet map.
const rectangles = [[51.49, -0.08], [51.5, -0.06]]
<Rectangle key={key} bounds={rectangle} color="green">
</Rectangle>
I want to add a text inside the rectangle like a label for the rectangle is there a way to do this?
I'm using react-leaflet library for this.
To write on the map, we can use a DivIcon from the Leaflet library added to a React-Leaflet Marker component.
Create a DivIcon with HTML
A DivIcon is an icon that can contain HTML instead of an image. We'll import the Leaflet library and create a DivIcon with our desired text.
import L from 'leaflet';
const text = L.divIcon({html: 'Your HTML text here'});
Add the DivIcon to a Marker
With the DivIcon created, we'll add it to a Marker placed in the center of a Polygon.
import React from 'react';
import L from 'leaflet';
import { Marker, Polygon } from 'react-leaflet';
const PolygonWithText = props => {
const center = L.polygon(props.coord).getBounds().getCenter();
const text = L.divIcon({html: props.text});
return(
<Polygon color="blue" positions={props.coords}>
<Marker position={center} icon={text} />
</Polygon>
);
}
export default PolygonWithText
Add the Marker to the Map
Finally, we add the Polygon, Marker, and DivIcon to a Map.
import React, { Component } from 'react';
import {Map, TileLayer} from 'react-leaflet';
import PolygonWithText from './PolygonWithText';
class MyMap extends Component {
render () {
return (
<Map center={[20.75, -156.45]} zoom={13}>
<TileLayer
attribution='&copy OpenStreetMap contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<PolygonWithText text="MyText" coords={[...]} />
</Map>
}
}
export default MyMap;
refer the below code, this one uses a rectangle with a tooltip inside it
{zoneLabel}
<Rectangle key={key} bounds={coordinates}>
</Rectangle>

Resources