ViewComponent

ViewComponent은 Ruby on Rails에서 재사용 가능하고 테스트 가능하며 캡슐화된 뷰 컴포넌트를 생성하기 위한 프레임워크로, Vue와 같은 JavaScript 프레임워크가 필요하지 않습니다. 이러한 컴포넌트들은 서버 측에서 렌더링되며 Haml과 같은 템플릿 언어와 원활하게 사용할 수 있습니다.

더 많은 정보는 공식 문서이 소개 비디오를 참조하세요.

Lookbook으로 컴포넌트 검색

개발 모드에서만 사용 가능한 http://gdk.test:3000/rails/lookbookLookbook을 사용하여 ViewComponent 미리보기를 탐색하고 상호 작용할 수 있습니다.

Pajamas 컴포넌트

저희 Pajamas 디자인 시스템의 일부 컴포넌트들은 app/components/pajamas에 ViewComponent로 사용할 수 있습니다.

참고: 이러한 컴포넌트들은 아직 생성 중이므로 모든 Pajamas 컴포넌트가 ViewComponent로 사용할 수 있는 것은 아닙니다. 원하는 컴포넌트를 찾을 수 없는 경우 Foundations team에 문의하세요.

사용 가능한 컴포넌트

이 목록은 최선을 다한 결과물입니다. 전체 목록은 app/components/pajamas에서 찾을 수 있습니다. 더 다양한 상호 작용 방법을 보려면 우리의 Lookbook인 http://gdk.test:3000/rails/lookbook도 참조하세요.

Alert

Pajamas::AlertComponentPajamas Alert 사양을 따릅니다.

예시:

기본적으로 이 코드는 닫을 수 있는 정보 alert와 아이콘을 생성합니다:

= render Pajamas::AlertComponent.new(title: "거의 완료되었습니다!")

여러 가지 변형, 아이콘 숨기기 등을 설정할 수도 있습니다:

= render Pajamas::AlertComponent.new(title: "완료되었습니다!",
  variant: :success,
  dismissible: :false,
  show_icon: false)

모든 옵션 목록은 해당 소스에서 확인할 수 있습니다.

Pajamas::BannerComponentPajamas Banner 사양을 따릅니다.

예시:

가장 간단한 형태의 배너 컴포넌트는 다음과 같습니다:

= render Pajamas::BannerComponent.new(button_text: '자세히 알아보기', button_link: example_path,
  svg_path: 'illustrations/example.svg') do |c|
  - c.with_title { '안녕, 세계!' }
  %p 여기에 배너 콘텐츠가 들어갑니다...

더 많은 제어를 원하는 경우, svg_path 대신 illustration 슬롯과 button_textbutton_link 대신 primary_action 슬롯을 사용할 수도 있습니다:

= render Pajamas::BannerComponent.new do |c|
  - c.with_illustration do
    = custom_icon('my_inline_svg')
  - c.with_title do
    안녕, 세계!
  - c.with_primary_action do
    = render 'my_button_in_a_partial'

모든 옵션 목록은 해당 소스에서 확인할 수 있습니다.

Button

Pajamas::ButtonComponentPajamas Button 사양을 따릅니다.

예시:

버튼 컴포넌트에는 많은 옵션이 있지만, 모두 좋은 기본값을 갖고 있습니다. 따라서 가장 간단한 버튼은 다음과 같습니다:

= render Pajamas::ButtonComponent.new do |c|
  = _('버튼 텍스트를 입력하세요')

다음 예시는 대부분의 사용 가능한 옵션을 보여줍니다:

= render Pajamas::ButtonComponent.new(category: :secondary,
  variant: :danger,
  size: :small,
  type: :submit,
  disabled: true,
  loading: false,
  block: true) do |c|
  버튼 텍스트를 입력하세요

<a> 태그와 유사한 모습의 버튼을 생성할 수도 있습니다:

= render Pajamas::ButtonComponent.new(href: root_path) do |c|
  집으로 이동

모든 옵션 목록은 해당 소스에서 확인할 수 있습니다.

Card

Pajamas::CardComponentPajamas Card 사양을 따릅니다.

예시:

카드에는 필수적으로 body 슬롯이 있고 headerfooter 슬롯은 선택적입니다:

= render Pajamas::CardComponent.new do |c|
  - c.with_header do
    헤더입니다.
  - c.with_body do
    %p 여러 줄
    %p 본문 내용입니다.
  - c.with_footer do
    여기에 푸터가 들어갑니다.

이러한 요소나 카드 자체에 사용자 정의 속성을 추가하려면 다음 옵션을 사용하십시오:

= render Pajamas::CardComponent.new(card_options: {id: "my-id"}, body_options: {data: { count: 1 }})

header_optionsfooter_options도 사용할 수 있습니다.

모든 옵션 목록은 해당 소스에서 확인할 수 있습니다.

체크박스 태그

Pajamas::CheckboxTagComponentPajamas Checkbox 사양을 따릅니다.

name 인수와 label 슬롯은 필수입니다.

예시:

= render Pajamas::CheckboxTagComponent.new(name: 'project[initialize_with_sast]',
  checkbox_options: { data: { testid: 'initialize-with-sast-checkbox', track_label: track_label, track_action: 'activate_form_input', track_property: 'init_with_sast' } }) do |c|
  - c.with_label do
    = s_('ProjectsNew|Enable Static Application Security Testing (SAST)')
  - c.with_help_text do
    = s_('ProjectsNew|Analyze your source code for known security vulnerabilities.')
    = link_to _('Learn more.'), help_page_path('user/application_security/sast/index'), target: '_blank', rel: 'noopener noreferrer', data: { track_action: 'followed' }

전체 옵션 목록은 다음의 소스를 참조하세요.

체크박스

Pajamas::CheckboxComponentPajamas Checkbox 사양을 따릅니다.

참고: Pajamas::CheckboxComponent는 내부적으로 GitLab UI 폼 빌더에서 사용되며, form 인수로 ActionView::Helpers::FormBuilder의 인스턴스가 필요합니다. 이 ViewComponent를 렌더링하려면 gitlab_ui_checkbox_component 메소드를 사용하는 것이 좋습니다. ActionView::Helpers::FormBuilder의 인스턴스 없이 체크박스를 사용하려면 CheckboxTagComponent를 사용하세요.

전체 옵션 목록은 다음의 소스를 참조하세요.

토글

Pajamas::ToggleComponentPajamas Toggle 사양을 따릅니다.

= render Pajamas::ToggleComponent.new(classes: 'js-force-push-toggle',
  label: s_("ProtectedBranch|Toggle allowed to force push"),
  is_checked: protected_branch.allow_force_push,
  label_position: :hidden)
  Leverage this block to render a rich help text. To render a plain text help text, prefer the `help` parameter.

참고: 토글 ViewComponent는 Vue.js 컴포넌트에 의존적인 특수한 성격을 가지고 있습니다. 이 컴포넌트를 실제로 초기화하려면 ~/toggles에서 initToggle 헬퍼를 호출해야 합니다.

전체 옵션 목록은 다음의 소스를 참조하세요.

레이아웃

레이아웃 컴포넌트는 GitLab에서 사용되는 일반적인 레이아웃 패턴을 만드는 데 사용할 수 있습니다.

사용 가능한 컴포넌트

수평 섹션

많은 설정 페이지에서 제목과 설명이 왼쪽에 있고 설정 필드가 오른쪽에 있는 레이아웃을 사용합니다. Layouts::HorizontalSectionComponent를 사용하여 이 레이아웃을 만들 수 있습니다.

예시:

= render ::Layouts::HorizontalSectionComponent.new(options: { class: 'gl-mb-6' }) do |c|
  - c.with_title { _('Naming, visibility') }
  - c.with_description do
    = _('Update your group name, description, avatar, and visibility.')
    = link_to _('Learn more about groups.'), help_page_path('user/group/index')
  - c.with_body do
    .form-group.gl-form-group
      = f.label :name, _('New group name')
      = f.text_field :name

전체 옵션 목록은 다음의 소스를 참조하세요.

최선의 방법

  • Haml에서 새로운 뷰를 만들려면 단순한 Haml 태그와 CSS 클래스를 만드는 대신 사용 가능한 컴포넌트를 사용하세요.
  • 기존 Haml 뷰를 수정하고 단순한 Haml로 구현된 버튼 등을 발견하면 해당 요소를 ViewComponent를 사용하도록 마이그레이션하는 것을 고려하세요.
  • 새로운 컴포넌트를 만들기로 결정한 경우, 해당 컴포넌트에 대한 미리보기도 만드는 것을 고려하세요. 이렇게 하면 다른 사람들이 Lookbook을 통해 컴포넌트를 발견하는 데 도움이 되며, 서로 다른 상태를 테스트하는 것이 훨씬 쉬워집니다.