Putting a div inside a link - area still clickable outside div - javascript

I put a div inside an a tag with an intent that only when I hover on the area occupied by the div, I will get the hand cursor.
But it has a strange behavior.
You can see that the div has fixed dimensions (red border).
But even if I move the mouse outside the div, still the hand cursor appears.
Why?
Like I said I basically want only the area inside the red border to be clickable.
Here is the code:
const AppCustomIcon = (props) => {
return (
<a target="blank"
href={props.url}>
<div style={{
height: 100,
width: 100,
display: "flex",
flexDirection: "column",
border:"1px solid red",
alignItems: "center"
}}>
<IconButton
style={{marginRight: 10}}
onClick={props.handleClick}
>
{props.icon}
</IconButton>
<Typography variant={"body1"}>{props.text}</Typography>
</div>
</a>
)
}

Anchor tag a by default is display: inline.
If you are applying styles to an element that contains flow elements or any other elements that are represented in CSS as display: block, you should set the itself to a proper block container type such as block or inline-block for its layout to work as intended.

Related

Align popover on clicked position React antd

<Popover
visible={visible}
content={content}
overlayInnerStyle={{ borderRadius: "8px", textAlign: "center" }}
placement="top"
>
<div
className="example-div"
id={exampleId}
contentEditable="true"
dangerouslySetInnerHTML={{ __html: styledExample }}
onClick={this.onHandleClick}
/>
</Popover>
Here Popover visibility has been managed using state.
I have added placement="top", as the div size is 100%, so Popover is getting populated on top middle of the div.
How can i align Popover placement exactly at clicked place instead of top middle.

How to align buttons in cards vertically in center in React using Material-UI?

I have made 3 cards in ReactJS using material-ui, so the problem I am facing is that the first card's paragraph is small and the second and third card's paragraph is a bit bigger so when I am placing my button the position of the button differ in all the 3 cards and because of that each paragraph buttons are not aligned properly in vertically center.
I have attached the image of the problem I am facing.
[1]: https://i.stack.imgur.com/ZkGVc.jpg
Here is the CodeSandBox link :-https://codesandbox.io/s/vigilant-sanderson-yp491?fontsize=14&hidenavigation=1&theme=dark
Below is my ReactJS and material-ui code
const Room = ({room}) => {
const classes = useStyles() ;
return (
<Card className={classes.root}className={classes.cards}>
<CardMedia className={classes.media} image={room.image} title={room.name}/>
<CardContent>
<div className={classes.CardContent}>
<h2>{room.name}</h2>
<Typography>
{room.description}
</Typography>
</div>
<Button variant="outlined" color="primary" justify="space-around">
Primary
</Button>
</CardContent>
<CardActions disableSpacing className={classes.CardActions}>
</CardActions>
</Card>
)
}
style.js
import { makeStyles } from '#material-ui/core/styles';
export default makeStyles(() => ({
media: {
height: 280,
},
cards:{
height:600
},
cardContent: {
display: 'flex',
justifyContent: 'space-between',
},
cardActions: {
justifyContent: 'space-between',
},
buttons: {
display: 'flex',
alignItems: 'center',
},
h5:{
fontfamily: 'Ubuntu',
},
}));
How can I align those buttons in each card vertically in the center while keeping the size of the paragraph the same?
As mentioned in the comment, if I understand well what you're trying to achieve, one possible solution would be to have the description have a fixed height. You can then add a scrollbar to it, and therefore the buttons will be aligned. Alternatively, instead of the scrollbar you could use a "show more" button to expand the content.
I have updated the codepen with an example using the browser native scrollbar - codepen
To enhance the solution you could look into using a slim scrollbar solution, such as react-perfect-scrollbar

images always getting smaller

I want to add images to my web site homepage but when i added it always getting smaller how can i cover my page with images?
Here is my code:
const Home = () => {
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "90vh",
}}
>
<img src={logo} alt="img" />
</div>
);
};
I tried object-fit:cover and width:100% but it doesnt work.
All images that i add seems like that:
Add style={{display: 'block', height: '100%'}} to the image and it will work.
By default <img> elements have display: inline. It doesn't matter how tall the parent is, it will try to adjust to the width of the parent which, in your case, is shrinked by the centering method.
i think that
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "90vh",
is breaking it, try to add width/height to your image

React-Sidebar: Button is unclickable when moved outside props.children

I'm trying to modify the first example given in the react-sidebar documentation by moving the location of the button outside of the props.children. When I do this, the button becomes unclickable. The button doesn't not just do anything, it actually can't be clicked.
It seems like the z-index from the sidebar is taking up the full screen even when it is collapsed, but when i set the button's z-index to 3 it is still unclickable.
render() {
return (
<Sidebar
sidebar={<b>Sidebar content</b>}
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
styles={{ sidebar: { background: "white" } }}
>
<button onClick={() => this.onSetSidebarOpen(true)}>
Open sidebar
</button>
</Sidebar>
);
}
My non-working modification:
render() {
return (
<div>
<Sidebar
sidebar={<b>Sidebar content</b>}
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
styles={{ sidebar: { background: "white" } }}
/>
// Moved button out of here
</Sidebar>
<button onClick={() => this.onSetSidebarOpen(true)}>
Open sidebar
</button>
</div>
);
}
This is because react-sidebar has an overlay which is covering the entire screen and the button is falling beneath it, because of which you can't click the button. Just give the button position: absolute and z-index: 4 or so and it will become clickable and will work.
The given answer doesn't work. What worked for me was setting the overlay's height and width to zero. Using Developer Console, you can find the CSS selector for the overlay, which, for me was #gatsby-focus-wrapper > div:nth-child(1). So, the code that fixed the issue was -
#gatsby-focus-wrapper > div:nth-child(1){
height: 0;
width: 0;
}

Why does React Native not offer a justify-self

I want to align an item in the primary axis. For example, I want to have a row with a few children all aligned left, and then one child aligned on the right side.
You could achieve that effect with something like "position: absolute; right: 0", but I'm wondering if theres a better way. It seems like there ought to be a justifySelf property, that only affects one child and affects its alignment on the primary axis, in the same way the alignSelf affects one child's alignment on the secondary axis.
Yet no such justifySelf seems to exist. Why is this?
It's similar to this question but not quite: How can you float: right in React Native?
I don't know React Native, but I do know flexbox!
Use the following code as a guide:
<div style="display: flex;">
<div>
I'll be on the left side
</div>
<div>
I'll be hugging the guy on the left side
</div>
<div>
I'll be hugging the guy hugging the guy on the left side
</div>
<div style="margin-left: auto;">
I'll be hugging the right side far away from those other guys
</div>
</div>
The margin set on the last child will push all other children to the left as far as their styles will allow, and push itself as far right as any other styles will allow.
You can test this out by also adding margin-right: auto; to the last child, and you will see the last child centered perfectly in the remaining space of the parent div, after the first three children take up their allotted space. This is because the competing "margin autos" will both share equally whatever space remains, since they can't cancel each other out and won't override each other.
Flex box was designed to handle margin spacing like this, so take advantage of it, as well as the other unique spacing options available under the justify-content property.
Helpful article: https://hackernoon.com/flexbox-s-best-kept-secret-bd3d892826b6
I believe you want to achieve something like this:
You can implement this by nesting views which share the same justifyContent property.
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'space-between',
}}>
<View>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
</View>
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
</View>
A partial answer: there's no justifySelf in React Native because there's no justify-self for flexbox in real CSS. There is a justify-self CSS property, but it doesn't do anything in flexbox layouts. You can see in the spec at https://drafts.csswg.org/css-align-3/#overview that justify-self is defined to apply to:
block-level boxes, absolutely-positioned boxes, and grid items
which notably doesn't include "flex items".
Okay, but why is this the case in CSS? MDN offers an explanation that you may or may not find satisfactory:
There is no justify-self in Flexbox
On the main axis Flexbox deals with our content as a group. The amount of space required to lay out the items is calculated, and the leftover space is then available for distribution. The justify-content property controls how that leftover space is used. Set justify-content: flex-end and the extra space is placed before the items, justify-content: space-around and it is placed either side of the item in that dimension, etc.
This means that a justify-self property does not make sense in Flexbox as we are always dealing with moving the entire group of items around.
On the cross axis align-self makes sense as we potentially have additional space in the flex container in that dimension, in which a single item can be moved to the start and end.
There's clearly some sense to this. It's always meaningful and coherent to put alignSelf: 'center' on a flex item to ask for it to be centered on the cross axis. But what would it mean to, for instance, put justifySelf: 'center' on a flex item, to ask for it to be centered on the main axis? How is that supposed to be handled if previous flex items have already filled more than half of the space along that axis?
Margins provide a fine solution for cases like yours. justifySelf, on the other hand, can't reasonably exist for flexbox, because the justifyContent values specify how to distribute flex items, and a little thought about what it would mean to instead apply them to individually specified items reveals that doing so is basically incoherent.
There is an easy way to do this without absolute positioning that works exactly how you'd expect with all items of varying heights lining up on the y-axis appropriately.
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ backgroundColor: 'green', height: 50, width: 50 }} />
<Text style={{flex: 1}}> text in here because why not!</Text>
<View style={{ backgroundColor: 'yellow', width: 30, height: 30 }} />
</View>
You can take a look at Flex Docs!
Adding flexDirection to a component's style determines the primary axis of its layout.
and then:
Adding alignItems to a component's style determines the alignment of children along the secondary axis (if the primary axis is row, then the secondary is column, and vice versa).
So your desired code will be:
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native';
export default class AlignItemsBasics extends Component {
render() {
return (
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
}}>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
</View>
);
}
};
// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => AlignItemsBasics);
UPDATE
If you mean something like this image:
Then I'll suggest you this:
import React, { Component } from "react";
import { View, StyleSheet } from "react-native";
class Playground extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.boxes} />
<View style={styles.boxes} />
<View
style={[
styles.boxes,
{
backgroundColor: "crimson",
position: "absolute",
right: 0
}
]}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center"
},
boxes: {
width: 50,
height: 50,
marginLeft: 1, // to separate each box!
backgroundColor: "steelblue"
}
});
export default Playground;
As far as i know with these props, it's the best way!
I really think the best way around this is to wrap whatever you're displaying inside a <View> with position: "absolute", and also add position: "absolute" to the element you want. Then, inside the <View>, add justifyContent: "center", and alignItems: "center". Hopefully this solves the issue.

Categories

Resources