ionic-team / ionic-framework

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.
https://ionicframework.com
MIT License
50.93k stars 13.51k forks source link

bug: action sheet, alert, picker, and toast do not open when isOpen is initially true #27928

Closed legendar closed 1 year ago

legendar commented 1 year ago

Prerequisites

Ionic Framework Version

v7.x

Current Behavior

I want to show IonToast on page load based on some condition, e.g:

const {isToastOpen} = useState(true)
...
<IonToast
  message="Some message"
  isOpen={isOpen}
>

if isOpen is initially true then a toast does not appear. It only appears if isOpen is initially false and then changed to true via timeout or any other event.

Expected Behavior

When I set isOpen={true} I expect that toast appears regardless of any other state.

Steps to Reproduce

Minimal STR is:

<IonToast
  message="Some message"
  isOpen={true}
>

In the provided repo the toast is rendered inside ExploreContainer here: https://github.com/legendar/ion-input-label-issue/blob/main/src/components/ExploreContainer.tsx#L23

Code Reproduction URL

https://github.com/legendar/ion-input-label-issue

Ionic Info

[WARN] Error loading @capacitor/ios package.json: Error: Cannot find module '@capacitor/ios/package.json'

   Require stack:
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/project/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/bin/ionic

[WARN] Error loading @capacitor/android package.json: Error: Cannot find module '@capacitor/android/package.json'

   Require stack:
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/project/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/bin/ionic

Ionic:

Ionic CLI : 7.1.1 (/home/***/.config/yarn/global/node_modules/@ionic/cli) Ionic Framework : @ionic/react 7.1.3

Capacitor:

Capacitor CLI : 5.2.1 @capacitor/android : not installed @capacitor/core : 5.2.1 @capacitor/ios : not installed

Utility:

cordova-res : not installed globally native-run : 1.7.2

System:

NodeJS : v16.15.0 (/home/***/.nvm/versions/node/v16.15.0/bin/node) npm : 8.5.5 OS : Linux 5.10

Additional Information

The following component can be used as a quick workaround:

function InlineToast({
  isOpen,
  ...otherProps
}): JSX.Element {
  const [isOpenInternal, setIsOpenInternal] = useState(false);

  useEffect(() => {
    // setTimeout is needed because sometimes the toast shadowDom is not ready
    setTimeout(() => {
      setIsOpenInternal(isOpen);
    }, 10);
  }, [isOpen]);

  return (
    <IonToast
      isOpen={isOpenInternal}
      {...otherProps}
    />
  );
}

In the provided repo replace IonToast with InlineToast component and the toast will appear.

liamdebeasi commented 1 year ago

Thanks for the report. It looks like some overlay components are missing logic in their didLoad lifecycle that opens the overlay if isOpen="true". For example, modal has the correct logic: https://github.com/ionic-team/ionic-framework/blob/a0b3ef02af718e232246515bb873ad8c090fa55d/core/src/components/modal/modal.tsx#L366-L368

Toast, picker, action sheet, and alert need this logic. Loading, modal, and popover are not impacted.

liamdebeasi commented 1 year ago

Here is a dev build with a proposed fix if you are interested in testing:

npm install @ionic/react@7.2.3-dev.11691156718.1638345c
legendar commented 1 year ago

Thanks for re quick response! I tested the fix on a sandbox and on a real project. It works fine!

liamdebeasi commented 1 year ago

Thanks for the issue. This has been resolved via https://github.com/ionic-team/ionic-framework/pull/27933, and a fix will be available in an upcoming release of Ionic Framework.

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.