Implementing drawer (Offcanvas) in react - reactjs

I am trying to implement Drawer (Offcanvas) component in react, so here the requirement is, there will be one Drawer whose component will change dynamically, consider something like bootstrap modal, you have one modal but you can trigger that modal from anywhere from the page just write data-toggle="modal" data-target="#modalid", but implementing this in react requires communicating between 2 root components & setting drawers component data from the triggering component or button. I have seen people added trigger & modal in same component & than passing state.toggle flag like below:
render() {
return (
<div>
<Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
<Drawer isOpen={this.state.drawer} toggle={this.toggle} className={this.props.className}>
<DrawerBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</DrawerBody>
</Drawer>
</div>
);
}
But in my case, there is no relationship between components, this component <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button> will be added in multiple places & there is no coupling between Drawer & Button. So how I can achieve this without using any state management like Redux or Flux or anything else

Related

Render custom components in React Markdown

I'm building my website documentation and I need to render custom component and it's props, such as <Callout>Hi! I'm a callout</Callout>.
I'm using contentlayer for the directory and react-markdown to render the content.
Here's my code.
<ReactMarkdown
rehypePlugins={[rehypeRaw]}
className={s.markdown}
components={{
code({ inline, children, ...props }) {
if (!inline) {
return (
<SyntaxHighlighter
style={coldarkDark}
language={'typescript'}
>
{String(children).replace(/\n$/, '')}
</SyntaxHighlighter>
)
}
return <code {...props}>{children}</code>
},
}}
>
{currentDoc.body}
</ReactMarkdown>
{currentDoc.body} is markdown. So, with this example, I'm passing this to <ReactMarkdown />:
Mollit nisi cillum exercitation minim officia velit laborum non Lorem
adipisicing dolore. Labore commodo consectetur commodo velit adipisicing irure
dolore dolor reprehenderit aliquip. Reprehenderit cillum mollit eiusmod
excepteur elit ipsum aute pariatur in. Cupidatat ex culpa velit culpa ad non
labore exercitation irure laborum.
<Callout>Hi! I'm a callout</Callout>
Hey! [link](https://github.com/rehypejs/rehype-react) right here
The idea is to catch your component Callout from your markdown and render it?
If so, I think i would use a custom "code language" in your markdown, and then parse it in yourcomponents callback from react-markdown.
For example, your markdown would be
Mollit nisi cillum exercitation minim officia velit laborum non Lorem
adipisicing dolore. Labore commodo consectetur commodo velit adipisicing irure
dolore dolor reprehenderit aliquip. Reprehenderit cillum mollit eiusmod
excepteur elit ipsum aute pariatur in. Cupidatat ex culpa velit culpa ad non
labore exercitation irure laborum.
```callout
Hi! I'm a callout
```
Something else....
React-markdown then render anything as code with a particular className using language-{code} which means in this case language-callout. So you need to catch it and render your callout depending on the className
code({ inline, className, children, ...props }) {
if (inline) return <code {...props}>{children}</code>
const value = String(children).replace(/\n$/, '');
if (className === "language-callout") return (
<Callout>{value}</Callout>
)
return (
<SyntaxHighlighter
style={coldarkDark}
language={'typescript'}
>
{value}
</SyntaxHighlighter>
)
}

Rendering react-bootstrap component causing errors

I am trying to add the react-bootstrap accordion to my react website. However, when I add it all I see is a white screen on the page that contains the accordion. I've installed react-bootstrap and bootstrap with npm. After refreshing the page I get the error:
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
import React, {Component} from 'react';
import Accordion from 'react-bootstrap/Accordion'
class Collapsible extends Component{
render() {
return(
<section className="container">
<Accordion>
<Accordion.Item eventKey="0">
<Accordion.Header>Accordion Item #1</Accordion.Header>
<Accordion.Body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</Accordion.Body>
</Accordion.Item>
</Accordion>
</section>
);
}
}
export default Collapsible;
You seem to be using version 4.x (1.6.x) of react-bootstrap, but the components you are using are in version 5.x (2.x) of react-bootstrap.
Version 4 Accordion
Has Accordion, Accordion.Toggle, and Accordion.Collapse components.
<Accordion defaultActiveKey="0">
<Card>
<Card.Header>
<Accordion.Toggle as={Button} variant="link" eventKey="0">
Click me!
</Accordion.Toggle>
</Card.Header>
<Accordion.Collapse eventKey="0">
<Card.Body>Hello! I'm the body</Card.Body>
</Accordion.Collapse>
</Card>
<Card>
<Card.Header>
<Accordion.Toggle as={Button} variant="link" eventKey="1">
Click me!
</Accordion.Toggle>
</Card.Header>
<Accordion.Collapse eventKey="1">
<Card.Body>Hello! I'm another body</Card.Body>
</Accordion.Collapse>
</Card>
</Accordion>
Version 5 Accordion
Has the Accordion, Item, Header, and Body components.
<Accordion defaultActiveKey="0">
<Accordion.Item eventKey="0">
<Accordion.Header>Accordion Item #1</Accordion.Header>
<Accordion.Body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</Accordion.Body>
</Accordion.Item>
<Accordion.Item eventKey="1">
<Accordion.Header>Accordion Item #2</Accordion.Header>
<Accordion.Body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</Accordion.Body>
</Accordion.Item>
</Accordion>
Here's a running codesandbox of your code running in version 2.0.0-beta.6 of react-bootstrap.

Panel.Heading color change

I have this part of code:
<Panel>
<Panel.Heading>
<Panel.Title toggle>
Hello
</Panel.Title>
</Panel.Heading>
<Panel.Collapse>
<Panel.Body>
Really wide body. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</Panel.Body>
</Panel.Collapse>
</Panel>
I wish to change Panel.Heading color, but there are no docs regarding this element. Anyone know how to do it?
Thanks in advance
To change the color of the heading for a panel, you can set the bsStyle prop of Panel, like the following:
<Panel bsStyle="primary">
<Panel.heading>
...
This will set the color of the top of the panel, based on Bootstrap's color styles.
Values you can use for bsStyle are:
default
primary
success
info
warning
danger
Here is a code sandbox that demonstrates this.
It's difficult to find documentation for this because react-bootstrap (and reactstrap) now use Bootstrap v4. Panel was a part of Bootstrap v3, but has now been replaced with Card. By digging around in some of the older versions of react-bootstrap (specifically their bs3-dev branch), you can look at how they used Panel back then.
You'll notice that, in the code sandbox, we need to add some older versions of the dependencies (bootstrap at 3.4.1 and react-bootstrap at 0.32.1).
Hope this helps!
Not familiar with react-bootstrap but just style with css? or
style={{backgroundColor:"anyColor"}}

How to use accordions in angular?

I have an accordion like the following. The issue I am having is if I click on "Section One", the accordion appears to expand, but in fact it refreshes the page and I cant collapse it. How to allow the accordion to open, but not cause a page refresh? (I would like the accordion to be able to open multiple sections at the same time)
<div class="accordion">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle-small" data-toggle="collapse" data-parent="#accordianParent" href="#sectionOne" style="border-left: 3px solid red;padding-left:10px;">
Section One
</a>
</div>
<div id="sectionOne" class="accordion-body collapse">
<div class="accordion-inner-small">
Here is all the wonderful stuff in section ONE.<br>
<br>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle-small" data-toggle="collapse" data-parent="#accordianParent" href="#sectionTwo">
Section Two
</a>
</div>
<div id="sectionTwo" class="accordion-body collapse">
<div class="accordion-inner-small">
Here is all the wonderful stuff in section TWO.<br>
<br>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
You are using the Bootstrap accordion and the href link that is in the accordion is not being intercepted by Angular and hence the full page load.
I suggest you look at UI Bootstrap or Angularstrap for accordion. Or else look at bootstrap docs to find if the behaviour can be supported without href.

How to make prettyphoto jquery lightbox responsive using Bootstrap v2.3.1

I have implemented lightbox in my responsive site using jquery prettyphoto lightbox and make my inline content to display inside lightbox.How to make the lightbox as responsive
<a href="#inline-1" rel="prettyPhoto" >Prettyphoto lightbox</a>
<div id="inline-1" class="hide">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></div>

Resources