3.10.112 • Published 10 months ago

@diotoborg/quaerat-dicta v3.10.112

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Styled React Modal

style: styled-components code style: prettier npm version npm downloads CircleCI codecov

For support for react <16.9, please use @diotoborg/quaerat-dicta@1.2.4.

For support for create-react-app <5.0.0, please import from @diotoborg/quaerat-dicta/build/umd.

Styled React Modal is built with styled-components. It uses the latest React 17.x features and exposes a familiar, easy to use API. It supports beforeOpen(), afterOpen(), and other lifecycle hooks so that animations can be handled easily. Unlike several other modal implementations in React, it does not pollute the DOM with excessive nodes.

Demo on CodeSandbox

Install

npm i -s @diotoborg/quaerat-dicta  # or use yarn

Usage

Add the <ModalProvider> component near the top of your application's tree.

import React from 'react'
import { ModalProvider } from '@diotoborg/quaerat-dicta'
...

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <ModalProvider>
        <FancyModalButton />
      </ModalProvider>
    </ThemeProvider>
  )
}

Use the <Modal> component.

For instructions on how the make your modal accessible according to the WAI-ARIA spec, see this CodeSandbox.

import Modal from '@diotoborg/quaerat-dicta'
...

const StyledModal = Modal.styled`
  width: 20rem;
  height: 20rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: ${props => props.theme.colors.white};
`

function FancyModalButton() {
  const [isOpen, setIsOpen] = useState(false)

  function toggleModal(e) {
    setIsOpen(!isOpen)
  }

  return (
    <div>
      <button onClick={toggleModal}>Click me</button>
      <StyledModal
        isOpen={isOpen}
        onBackgroundClick={toggleModal}
        onEscapeKeydown={toggleModal}>
        <span>I am a modal!</span>
        <button onClick={toggleModal}>Close me</button>
      </StyledModal>
    </div>
  )
}

API

Top-Level Exports

  • <ModalProvider>
  • Modal (Default)
    • Modal.styled(styles)
    • <Modal>
  • <BaseModalBackground>

<ModalProvider>

Sets the root portal where <Modal>s will be rendered.

Props

  • backgroundComponent (Component): A styled component to be used as the default modal background. If not provided, library defaults will be used.

Example:

import { ModalProvider } from '@diotoborg/quaerat-dicta'

const SpecialModalBackground = styled.div`
  display: flex;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 30;
  opacity: ${props => props.opacity};
  background-color: green;
`

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <ModalProvider backgroundComponent={SpecialModalBackground}>
        <FancyModalButton />
      </ModalProvider>
    </ThemeProvider>
  )
}

Modal.styled(styles)

Factory method that accepts a tagged template literal and returns a <Modal> component with styles included.

Arguments

  • styles (Tagged Template Literal): styled-components compatible css styles.

Example:

const StyledModal = Modal.styled`
  width: 20rem;
  height: 20rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: ${props => props.theme.colors.white};
`

<Modal>

Renders its children in a modal when open, nothing when not open.

Props

  • isOpen (Boolean): A boolean that indicates whether the modal is to be open or closed.
  • onBackgroundClick (Function): A function that is called when the modal background is clicked.
  • onEscapeKeydown (Function): A function that is called when the escape key is pressed while the modal is open.
  • backgroundProps (Object): A props object that is spread over the backgroundComponent when included.
  • allowScroll (Boolean): When true, scrolling in the document body is not disabled when the modal is open.
  • beforeOpen (Function): A function that is called before the modal opens. If this function returns a promise, then the modal is opened after the promise is resolved.
  • afterOpen (Function): A function that is called after the modal opens.
  • beforeClose (Function): A function that is called before the modal closes. If this function returns a promise, then the modal is closed after the promise is resolved.
  • afterClose (Function): A function that is called after the modal closes.

Example:

import Modal from '@diotoborg/quaerat-dicta'

function FancyModalButton() {
  const [isOpen, setIsOpen] = useState(false)

  function toggleModal(e) {
    setIsOpen(!isOpen)
  }

  return (
    <div>
      <button onClick={toggleModal}>Click me</button>
      <Modal
        isOpen={isOpen}
        onBackgroundClick={toggleModal}
        onEscapeKeydown={toggleModal}>
        <span>I am a modal!</span>
        <button onClick={toggleModal}>Close me</button>
      </Modal>
    </div>
  )
}

<BaseModalBackground>

A convenience base component for making default background styles with <ModalProvider>.

Example:

const SpecialModalBackground = styled(BaseModalBackground)`
  background-color: green;
`
flagkinesistslibtranspilerpostcss-pluginerrorroute53momentdeepreusecompilerwarningreworkdom$.extendeventDispatcherpopmotiontc39String.prototype.matchAllmatchAllwalkingassertswritablefromstylesObject.fromEntriesreduceECMAScript 3ECMAScript 2015inferencermio-tscommand-lineArrayBuffer#slicetrimStartqueryRxfastoutputRxJSECMAScript 2016eventsFunction.prototype.nameapimetadatanativedataviewes7toolkitpipeestreedayjsdataViewutilitiesjstypeerrorviewkoreanrulescjkswftrimEndwaitautoprefixerreadablestreamBigUint64ArrayregexUint32Arraydelete256qswgetdebughttpES2017jasmineforEachInt16ArrayTypeScripttyped arrayuuidtakeavanegative zeroES2019core-jsUint8ArrayreverseddircomparedeterministicbyterssjapaneseCSSStyleDeclarationdeepcopyframeworktermTypeBoxwhichinputTypedArrayjsdiffECMAScript 2022keyhelpersutil.inspectajaxloadbalancingmaphardlinksglobalstringifytelephonepositiveuninstalldefinetypesafecommanderfull-widthajvcloudformationlintindicatorinstallObject.assignstyleguideelectrondom-testing-librarymoduleReflect.getPrototypeOfES5sinatraexpressproxytypedarrayharmonyvariablescryptoobjtranspilepluginUnderscoregetintrinsicregular-expressioncomputed-typesgesturesreact animationsidechaikarmadynamodbcallredirecthashRegExp#flagsyupgitignorestreamsasyncnpmfast-deep-cloneformcodeschannelES2023createshellJSONawesomesauceequalityfeedexecutematchesimmutableclassnamesymlinkseventEmittersetterescall-boundrandomURLSearchParamscoverageECMAScript 5ES2020constconcatMapSystem.globalrobustfilees2016superagentreact-testing-libraryelasticacheartfolderfile systemespree__proto__containsclientlast[[Prototype]]tester.gitignoreMicrosofttimeObservableES2016configurablepackage.jsonECMAScript 2019valuestypedarraysmake dirpropfind-upInt8ArrayoptimizerreversehasOwntoobjectcacheObject.entriesdebuggerdefinePropertyyamltapenvironmentsdateglobalThiscss-in-jsomitasciiarktypesearchreadablegdprarraytestArrayBufferramdasliceruntimeponyfillpushtoReversedchildownebsString.prototype.trimpicomatchfast-copyhtmlbinenvironmenttransformshimsafecollection.es6WeakMapsymbolhelperwindowexpressiontostringtagutilsfindupfigletmanipulationvisualcorenpmignoreargvlockfilepinoeslinttypedArray.prototype.containseast-asian-widthpostcssunicodeagentpoint-freeprotocol-buffersInt32Arraydataredactregular expressiondeep-clonerm -frconfigsymbolsspeceslintconfigassertcharacterseslintpluginjsonschemaa11yenvschemeimportreduceri18nsuperstructvarsnegativemkdirsbeanstalkjson-schema-validationidentifiersescapewhatwglogimmeres2018execfileurlform-validationArray.prototype.flatMappackagescircularterminalprettyfnmatchroutesharedarraybufferquerystringredux-toolkitshebangparseworkspace:*react posedescriptionclasseslocaldeep-copyvpcPromisemulti-packageguidcolourMapimportexportArrayformatglobgrouputilsetlinkcssclass-validatorObject.valuestacitsqsstringifiercloudfrontArray.prototype.flatdotenvtypeofUint8ClampedArrayrequiresomepurecolumnsURLgetPrototypeOfloggingarraysES2022widthglobalsfunction.lengthlibphonenumberArray.prototype.includessettingsfunctionsl10nincludescommandwaftraverseserializerprefixes-abstractdescriptorECMAScript 2020valid6to5appweakmapmergeArray.prototype.findLasthasglaciershamtransportflagsjshintinvariantlistenersstyleinternal slotutilityiteratepreprocessorframerrm -rfecmascriptes2017weaksetenderes6loadingspeedbannerpropertiessubprocessprototypeshrinkwrapAsyncIteratorfindmochaflattennamesnodeglobal this valuerangeerrorasterisksRFC-6455protobufSymboleffect-tsconsolefindLastprivateposespringcloudwatchslotautoscalingObservablesperformantES6browserfast-deep-copylanguagesetImmediatewebapollorouterloggerObject.getPrototypeOfSetthreestylingES8WebSocketarraybufferxmlvalidateparentsemitfsgettertypescript0rdsFloat64Arraytddcall-bindcolorsArrayBuffer.prototype.sliceacornArray.prototype.findLastIndexRegExp.prototype.flagsaccessibilityclonewaapimkdirstoragegatewaydirectorydrophookformECMAScript 7recursivespawnnumberworkerrestfulcode pointsES2021moveStreamphone@@toStringTagbabelWeakSetminimalchineseObject.keysSymbol.toStringTaghttpsignoreperformancesameValueZerobrowserslistbyteLengthlooksnsstarterPushjsdomjavascriptmrutoolscolorspinnerspinnersjsoninternalregexpchromiumanimationttyzodStreamsbrowserlistjson-schemaArray.prototype.flattenregular expressionsjQueryairbnbES7rgbsortedassignfunctionaltrimRightcallbindtestingfpbusybinaryoffsetpackage managermodulesbyteOffsetECMAScript 6binariesec2collectionquotestructuredClonedependenciesidformsFloat32ArrayECMAScript 2018varselfhasOwnPropertyiteratorhas-ownstableformattinges-shimsistypereact-hooksmapreducegenericsgroupByxtermes-shim APIremovehooksfantasy-landinstallergetsource mapreadECMAScript 2023ES2015locationentriesgraphqllengtheverynodejses2015syntaxerrorprocesstapeECMAScript 2021reactvestfastcopyawspnpm9YAMLcensorprunerouting-0flatMapupoptionastmkdirpbufferes8typesjsxiecloudtrailcurriedcurles5
3.10.111

10 months ago

3.10.112

10 months ago

3.10.110

10 months ago

3.10.109

10 months ago

3.10.108

10 months ago

3.10.107

10 months ago

3.10.106

10 months ago

3.10.105

10 months ago

3.10.104

10 months ago

2.10.104

10 months ago

2.10.103

10 months ago

2.9.103

10 months ago

2.9.102

10 months ago

2.9.101

10 months ago

2.9.100

10 months ago

2.9.99

10 months ago

2.9.98

10 months ago

2.9.97

10 months ago

2.9.96

10 months ago

2.9.95

10 months ago

2.9.94

10 months ago

2.9.93

10 months ago

2.9.92

11 months ago

2.9.91

11 months ago

2.9.90

11 months ago

2.9.89

11 months ago

2.9.88

11 months ago

2.9.87

11 months ago

2.9.86

11 months ago

2.9.85

11 months ago

2.9.84

11 months ago

2.9.83

11 months ago

2.9.82

11 months ago

2.9.81

11 months ago

2.9.80

11 months ago

2.9.79

11 months ago

2.9.78

11 months ago

2.9.77

11 months ago

2.8.77

11 months ago

2.8.76

11 months ago

2.8.75

11 months ago

2.8.74

11 months ago

2.8.73

11 months ago

2.8.72

11 months ago

2.8.71

11 months ago

2.8.70

11 months ago

2.8.69

11 months ago

2.8.68

11 months ago

2.8.67

11 months ago

2.8.66

11 months ago

2.8.65

11 months ago

2.8.64

11 months ago

2.8.63

12 months ago

2.8.62

12 months ago

2.7.62

12 months ago

2.7.61

12 months ago

2.7.60

12 months ago

2.7.59

12 months ago

2.7.58

12 months ago

2.7.57

12 months ago

2.7.56

12 months ago

2.7.55

12 months ago

2.7.54

12 months ago

2.6.54

12 months ago

2.5.54

12 months ago

2.5.53

12 months ago

2.5.52

12 months ago

2.5.51

12 months ago

2.5.50

12 months ago

2.5.49

12 months ago

2.5.48

12 months ago

2.5.47

12 months ago

2.5.46

1 year ago

2.5.45

1 year ago

2.5.44

1 year ago

2.5.43

1 year ago

2.5.42

1 year ago

2.5.41

1 year ago

2.5.40

1 year ago

2.5.39

1 year ago

2.5.38

1 year ago

2.5.37

1 year ago

2.5.36

1 year ago

2.5.35

1 year ago

2.5.34

1 year ago

2.4.34

1 year ago

2.4.33

1 year ago

2.4.32

1 year ago

2.4.31

1 year ago

2.4.30

1 year ago

2.4.29

1 year ago

2.4.28

1 year ago

2.4.27

1 year ago

2.4.26

1 year ago

2.3.26

1 year ago

2.3.25

1 year ago

2.3.24

1 year ago

2.3.23

1 year ago

2.3.22

1 year ago

2.3.21

1 year ago

2.2.21

1 year ago

2.2.20

1 year ago

2.2.19

1 year ago

2.2.18

1 year ago

2.2.17

1 year ago

2.2.16

1 year ago

2.2.15

1 year ago

2.2.14

1 year ago

1.2.14

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago