Skip to content

Commit ff6205b

Browse files
committed
Clarify how Modal and FormLayout can be composed together
1 parent d4ea296 commit ff6205b

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

src/lib/components/Modal/README.mdx

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from 'docz'
1515
import {
1616
Button,
17+
CheckboxField,
1718
FormLayout,
1819
Modal,
1920
ModalBody,
@@ -666,57 +667,61 @@ On top of that, the modal can adjust to the width of its content.
666667
}}
667668
</Playground>
668669

669-
👉 Please note the auto width may not function correctly in combination with
670-
other auto-layout mechanisms, e.g. the auto-width
671-
[FormLayout](/components/form-layout#label-width). It's just too much
672-
magic that doesn't work together (yet?) 🎩.
670+
## Position
673671

674-
👉 Beware of horizontal FormLayout inside `small` modals. While automatic
675-
overflow handling comes to the rescue in this kind of scenario, you will be
676-
better off with the combination of auto-sized modal and horizontal FormLayout
677-
with a fixed label width (i.e. any other than `auto`, see the previous note).
672+
Modal can be aligned either to the top or center of the screen.
678673

679674
<Playground>
680675
{() => {
681676
const [modalOpen, setModalOpen] = React.useState(false);
677+
const [modalPosition, setModalPosition] = React.useState('center');
682678
const modalPrimaryButtonRef = React.useRef();
683679
const modalCloseButtonRef = React.useRef();
684680
return (
685681
<>
686682
<Button
687-
label="Launch auto-with modal with a form"
688-
onClick={() => setModalOpen(true)}
683+
label="Launch modal at center"
684+
onClick={() => {
685+
setModalPosition('center');
686+
setModalOpen(true);
687+
}}
688+
/>
689+
<Button
690+
label="Launch modal at top"
691+
onClick={() => {
692+
setModalPosition('top');
693+
setModalOpen(true);
694+
}}
689695
/>
690696
<div>
691697
{modalOpen && (
692698
<Modal
693699
closeButtonRef={modalCloseButtonRef}
700+
position={modalPosition}
694701
primaryButtonRef={modalPrimaryButtonRef}
695-
size="auto"
696702
>
697703
<ModalHeader>
698-
<ModalTitle>Form inside modal</ModalTitle>
704+
<ModalTitle>Delete the user?</ModalTitle>
699705
<ModalCloseButton onClick={() => setModalOpen(false)} />
700706
</ModalHeader>
701707
<ModalBody>
702708
<ModalContent>
703-
<FormLayout fieldLayout="horizontal">
704-
<TextField label="A form element" />
705-
<TextField label="Another form element" />
706-
<TextField label="Yet another one" />
707-
</FormLayout>
709+
<p>
710+
Do you really want to delete the user <code>admin</code>?
711+
This cannot be undone.
712+
</p>
708713
</ModalContent>
709714
</ModalBody>
710715
<ModalFooter>
711716
<Button
712-
color="primary"
713-
label="Save"
717+
color="danger"
718+
label="Delete"
714719
onClick={() => setModalOpen(false)}
715720
ref={modalPrimaryButtonRef}
716721
/>
717722
<Button
718723
color="secondary"
719-
label="Cancel"
724+
label="Close"
720725
onClick={() => setModalOpen(false)}
721726
priority="outline"
722727
ref={modalCloseButtonRef}
@@ -730,61 +735,64 @@ with a fixed label width (i.e. any other than `auto`, see the previous note).
730735
}}
731736
</Playground>
732737

733-
## Position
738+
## Forms in Modals
734739

735-
Modal can be aligned either to the top or center of the screen.
740+
You can safely place a FormLayout into a Modal of any size, including the
741+
auto-width Modal.
736742

737743
<Playground>
738744
{() => {
739745
const [modalOpen, setModalOpen] = React.useState(false);
740-
const [modalPosition, setModalPosition] = React.useState('center');
746+
const [agree, setAgree] = React.useState(true);
741747
const modalPrimaryButtonRef = React.useRef();
742748
const modalCloseButtonRef = React.useRef();
743749
return (
744750
<>
745751
<Button
746-
label="Launch modal at center"
747-
onClick={() => {
748-
setModalPosition('center');
749-
setModalOpen(true);
750-
}}
751-
/>
752-
<Button
753-
label="Launch modal at top"
754-
onClick={() => {
755-
setModalPosition('top');
756-
setModalOpen(true);
757-
}}
752+
label="Launch auto-width modal with auto-width form"
753+
onClick={() => setModalOpen(true)}
758754
/>
759755
<div>
760756
{modalOpen && (
761757
<Modal
762758
closeButtonRef={modalCloseButtonRef}
763-
position={modalPosition}
764759
primaryButtonRef={modalPrimaryButtonRef}
760+
size="auto"
765761
>
766762
<ModalHeader>
767-
<ModalTitle>Delete the user?</ModalTitle>
763+
<ModalTitle>Auto-width form inside auto-width modal</ModalTitle>
768764
<ModalCloseButton onClick={() => setModalOpen(false)} />
769765
</ModalHeader>
770766
<ModalBody>
771767
<ModalContent>
772-
<p>
773-
Do you really want to delete the user <code>admin</code>?
774-
This cannot be undone.
775-
</p>
768+
<FormLayout autoWidth fieldLayout="horizontal">
769+
<TextField
770+
label="A form element"
771+
validationState="warning"
772+
validationText={`Account with this name already exists,
773+
pick a different one.`
774+
}
775+
/>
776+
<TextField label="Another form element" />
777+
<TextField label="Yet another one" />
778+
<CheckboxField
779+
checked={agree}
780+
label="I agree"
781+
onChange={() => setAgree(!agree)}
782+
/>
783+
</FormLayout>
776784
</ModalContent>
777785
</ModalBody>
778786
<ModalFooter>
779787
<Button
780-
color="danger"
781-
label="Delete"
788+
color="primary"
789+
label="Save"
782790
onClick={() => setModalOpen(false)}
783791
ref={modalPrimaryButtonRef}
784792
/>
785793
<Button
786794
color="secondary"
787-
label="Close"
795+
label="Cancel"
788796
onClick={() => setModalOpen(false)}
789797
priority="outline"
790798
ref={modalCloseButtonRef}
@@ -798,6 +806,14 @@ Modal can be aligned either to the top or center of the screen.
798806
}}
799807
</Playground>
800808

809+
👉 Inside Modal, we recommend using the `autoWidth` option of FormLayout. This
810+
prevents the Modal from unwanted horizontal expansion when a long validation
811+
text pops up during user's interaction with the form.
812+
813+
👉 Beware of horizontal FormLayout inside `small` modals. While automatic
814+
overflow handling comes to the rescue in this kind of scenario, you will be
815+
better off with the combination of auto-sized modal and horizontal FormLayout.
816+
801817
## Keyboard Control
802818

803819
Modal can be controlled either by mouse or keyboard. To enhance user
@@ -809,8 +825,8 @@ To enable it, you just need to pass a reference to the buttons using
809825
a reference to the button is that if the button is disabled, the key press will
810826
not fire the event.
811827

812-
👉 We strongly recommend using this feature together with Autofocus for a better
813-
user experience.
828+
👉 We strongly recommend using this feature together with
829+
[Autofocus](#autofocus) for a better user experience.
814830

815831
## Autofocus
816832

0 commit comments

Comments
 (0)