Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#56496] Document Primer Flash Modals Pattern

Kabiru Mwenjaaad38c3684e0
diff --git a/lookbook/docs/patterns/09-flash-modal.md.erb b/lookbook/docs/patterns/09-flash-modal.md.erb
new file mode 100644
index 00000000000..1829e02d808
--- /dev/null
+++ b/lookbook/docs/patterns/09-flash-modal.md.erb
@@ -0,0 +1,45 @@
+This pattern defines use cases where a modal view component is rendered on the next request via a [flash](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html
+). This is useful when you want to display a modal after a user action, such as a form submission or on a callback request from an external service.
+
+> *_[Flash](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html) provides a way to pass temporary primitive types between controller actions. Anything you place in the flash is stored in the session and will be exposed to the very next action and then cleared out._
+
+## Overview
+
+To flash a modal, set a `flash[:op_modal]` hash in your controller action. The hash should contain the following keys:
+ * `type:` - the modal component to render
+ * `parameters:` - the parameters to pass to the modal component
+
+Ex.
+
+```ruby
+flash[:op_modal] = {
+ type: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,
+ parameters: { storage: storage.id }
+}
+```
+
+Internally, the `FlashMessagesHelper#render_flash_messages` will select flash messages with the key `:op_modal` and render the modal component with the given parameters.
+
+### Flash a modal on callback from external service
+
+When you want to display a modal after a callback request from an external service, set the `:op_modal` properties directly in the `session` in the controller action that performs the open redirect and then extract them in the controller action that handles the callback.
+
+Ex.
+
+```ruby
+# Controller action that performs the open redirect
+def redirect_to_external_service
+ session[:op_modal] = {
+ type: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,
+ parameters: { storage: storage.id }
+ }
+ redirect_to external_service_url
+end
+
+# Controller action that handles the callback
+def callback_from_external_service
+ flash[:op_modal] = session.delete(:op_modal)
+ # Handle the callback
+end
+```
+