question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

💡 Proposal - ModalLauncher converted to a hook

See original GitHub issue

Forma 36 contribution proposal

The problem

The modal pattern is frequently used, but the API of the ModalLauncher util is a bit complex to use. This is a very basic example, but if you look for ModalLauncher in Contentful’s webapp you can find very complex ones

import {
  Modal,
  ModalLauncher,
} from '@contentful/forma-36-react-components';

const openMyModal = () => {
  return ModalLauncher.open(({ isShown, onClose }) => (
    <Modal
      isShown={isShown}
      onClose={() => onClose()}>
      {/* ... */}
    </Modal>
  ));
};

function App() {
  return <button onClick={openMyModal}>click me to open the modal</button>
}

The proposed solution

I think we could use some inspiration from the hooks Chakra UI give to their users, specifically the useDisclosure hook which is used for components that “open” and “close” and create a hook that handles the open/close state of the modal and also the callbacks like onClose and then we would only need to do something like this:

import {
  Modal,
  useDisclosure,
} from '@contentful/forma-36-react-components';

function App() {
  const { isShown, onOpen, onClose } = useDisclosure()

  return (
    <>
      {/* onOpen already sets isShown to true */}
      <button onClick={onOpen}>Open Drawer</button>
      
       <Modal
         isShown={isShown}
         onClose={onClose}> {/* onClose already sets isShown to false */}
         {/* ... */}
       </Modal>
    </>
  )
}

Breaking changes

  • deprecation of ModalLauncher

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gui-santoscommented, Mar 17, 2021

I would agree to have this as an alternative approach but I would not deprecate ModalLauncher. It’s a widely used pattern and it’s used in a LOT of places and people are quite happy with the API.

@suevalov I would then just reject this Proposal, IMO having two ways of doing the same thing is just increasing the confusion and maintainability of the Library

0reactions
suevalovcommented, Mar 17, 2021

Yeah, IMO the cost of upgrading to the new API is extremely high (especially in the field-editors), so I would be really afraid of removing ModalLauncher.

Let’s close the proposal in this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ModalLauncher - Forma 36
ModalLauncher is a utility that allows calling Modal and ModalConfirm in an imperative code, ... import { ModalLauncher } from '@contentful/f36-components';.
Read more >
Where to use a hook? - Write Better Proposals
Obvious places for a hook include any high-level summary of the entire proposal. Depending on the funder, this may be called an executive ......
Read more >
HOOK the CLIENT with these Upwork Proposal Tips! - YouTube
HOW do you get hired on Upwork? With an AWESOME proposal that the client can't resist! I've got 5 tips for you today...
Read more >
Proposal: useNavigationEffect() hook · Issue #9 - GitHub
To be honest the main problem is naming the two hooks to clarify if the effect is called inside the transitions or outside....
Read more >
Carey Hook - Applications Proposal Engineer - LinkedIn
View Carey Hook's profile on LinkedIn, the world's largest professional community. Carey has 15 jobs listed on their profile. See the complete profile...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found