Skip to content

Commit

Permalink
add a component for zen mode button
Browse files Browse the repository at this point in the history
  • Loading branch information
bsatarnejad committed Jan 24, 2024
1 parent 3b5b110 commit f7b0f7f
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/components/primer/open_project/zen_mode_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= render(Primer::BaseComponent.new(tag: "zen-mode-button", **@system_arguments)) do %>
<%= render(
Primer::Beta::IconButton.new(
scheme: :default,
id: 'zenModeButton',
icon: 'screen-normal',
aria: { label: 'zen mode' },
autofocus: true,
data: { target: "zen-mode-button.button", action: "click:zen-mode-button#performAction" }
)
) %>
<% end %>
5 changes: 5 additions & 0 deletions app/components/primer/open_project/zen_mode_button.pcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* CSS for ZenModeButton */

.zen-mode-button--active {
box-shadow: 0 0 3px var(--color-border-default) inset;
}
23 changes: 23 additions & 0 deletions app/components/primer/open_project/zen_mode_button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Primer
module OpenProject
# Add a general description of component here
# Add additional usage considerations or best practices that may aid the user to use the component correctly.
# @accessibility Add any accessibility considerations
class ZenModeButton < Primer::Component
status :open_project

# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(**system_arguments)
@system_arguments = system_arguments
@system_arguments[:tag] = "zen-mode-button"
@system_arguments[:classes] =
class_names(
@system_arguments[:classes],
"zen-mode-button"
)
end
end
end
end
48 changes: 48 additions & 0 deletions app/components/primer/open_project/zen_mode_button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {attr, controller, target, targets} from '@github/catalyst'
import screenfull from 'screenfull';


@controller
class ZenModeButtonElement extends HTMLElement {
@target button: HTMLElement
inZenMode = false;
@attr name = 'screen-full';

private deactivateZenMode():void {
this.inZenMode = false;
this.button.classList.remove('zen-mode-button--active');
this.querySelector('body')?.classList.remove('zen-mode');
if (screenfull.isEnabled && screenfull.isFullscreen) {
screenfull.exit();
}
}

private activateZenMode() {
this.inZenMode = true;
document.querySelector('body')?.classList.add('zen-mode');
this.button.classList.add('zen-mode-button--active');
if (screenfull.isEnabled) {
screenfull.request();
}
}

public performAction() {
if (this.inZenMode) {
this.deactivateZenMode();
} else {
this.activateZenMode();
}
}
}

declare global {
interface Window {
ZenModeButtonElement: typeof ZenModeButtonElement
}
}

if (!window.customElements.get('zen-mode-button')) {
window.ZenModeButtonElement = ZenModeButtonElement
window.customElements.define('zen-mode-button', ZenModeButtonElement)
}

1 change: 1 addition & 0 deletions app/components/primer/primer.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@
@import "./open_project/drag_handle.pcss";
@import "./open_project/border_grid.pcss";
@import "./open_project/input_group.pcss";
@import "./open_project/zen_mode_button.pcss";
1 change: 1 addition & 0 deletions app/components/primer/primer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ import '../../../lib/primer/forms/primer_multi_input'
import '../../../lib/primer/forms/primer_text_field'
import '../../../lib/primer/forms/toggle_switch_input'
import './alpha/action_menu/action_menu_element'
import './open_project/zen_mode_button'
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"@github/relative-time-element": "^4.0.0",
"@github/tab-container-element": "^3.1.2",
"@oddbird/popover-polyfill": "^0.3.6",
"@primer/behaviors": "^1.3.4"
"@primer/behaviors": "^1.3.4",
"screenfull": "^6.0.2"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
Expand Down
19 changes: 19 additions & 0 deletions previews/primer/open_project/zen_mode_button_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# Setup Playground to use all available component props
# Setup Features to use individual component props and combinations

module Primer
module OpenProject
# @label ZenModeButton
class ZenModeButtonPreview < ViewComponent::Preview
def playground()
render(Primer::OpenProject::ZenModeButton.new())
end
# @label Default
def default()
render(Primer::OpenProject::ZenModeButton.new())
end
end
end
end
3 changes: 3 additions & 0 deletions static/classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -677,5 +677,8 @@
],
"UnderlineNav-octicon": [
"Primer::Alpha::UnderlineNav"
],
"Zone-mode-button": [
"Primer::OpenProject::ZoneModeButton"
]
}
1 change: 1 addition & 0 deletions test/components/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PrimerComponentTest < Minitest::Test

# Components with any arguments necessary to make them render
COMPONENTS_WITH_ARGS = [
[Primer::OpenProject::ZenModeButton, {}],
[Primer::OpenProject::InputGroup, {}, proc { |component|
component.with_text_input(name: "a name", label: "My input group", value: "Copyable value")
component.with_trailing_action_clipboard_copy_button(id: "button", value: "Copyable value", aria: { label: "Copy some text" })
Expand Down
13 changes: 13 additions & 0 deletions test/components/primer/open_project/zen_mode_button_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "components/test_helper"

class PrimerOpenProjectZenModeButtonTest < Minitest::Test
include Primer::ComponentTestHelpers

def test_renders
render_inline(Primer::OpenProject::ZenModeButton.new)

assert_selector(".zen-mode-button .octicon-screen-normal")
end
end
11 changes: 11 additions & 0 deletions test/system/open_project/zen_mode_button_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "system/test_case"

class IntegrationOpenProjectZenModeButtonTest < System::TestCase
def test_renders_component
visit_preview(:default)

assert_selector(".zen-mode-button")
end
end

0 comments on commit f7b0f7f

Please sign in to comment.