Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Primer::Image to Primer::Alpha::Image #1527

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-jars-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

deprecating Primer::Image in favor of Primer::Alpha::Image
50 changes: 50 additions & 0 deletions app/components/primer/alpha/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Primer
module Alpha
# Use `Image` to render images.
#
# @accessibility
# Always provide a meaningful `alt`.
class Image < Primer::Component
status :alpha

# @example Default
#
# <%= render(Primer::Alpha::Image.new(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub")) %>
#
# @example Helper
#
# <%= primer_image(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub") %>
#
# @example Lazy loading
#
# <%= render(Primer::Alpha::Image.new(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub", lazy: true)) %>
#
# @example Custom size
#
# <%= render(Primer::Alpha::Image.new(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub", height: 100, width: 100)) %>
#
# @param src [String] The source url of the image.
# @param alt [String] Specifies an alternate text for the image.
# @param lazy [Boolean] Whether or not to lazily load the image.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(src:, alt:, lazy: false, **system_arguments)
@system_arguments = deny_tag_argument(**system_arguments)

@src = src
@system_arguments[:tag] = :img
@system_arguments[:alt] = alt

return unless lazy

@system_arguments[:loading] = :lazy
@system_arguments[:decoding] = :async
end

def call
render(Primer::BaseComponent.new(src: image_path(@src), **@system_arguments))
end
end
end
end
6 changes: 3 additions & 3 deletions app/components/primer/beta/blankslate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Blankslate < Primer::Component
# Use:
#
# - `visual_icon` for an <%= link_to_component(Primer::OcticonComponent) %>.
# - `visual_image` for an <%= link_to_component(Primer::Image) %>.
# - `visual_image` for an <%= link_to_component(Primer::Alpha::Image) %>.
# - `visual_spinner` for a <%= link_to_component(Primer::SpinnerComponent) %>.
#
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
Expand All @@ -40,7 +40,7 @@ class Blankslate < Primer::Component
system_arguments[:size] = "56x56"
system_arguments[:classes] = class_names("blankslate-image", system_arguments[:classes])

Primer::Image.new(**system_arguments)
Primer::Alpha::Image.new(**system_arguments)
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ class Blankslate < Primer::Component
system_arguments[:size] = :medium
system_arguments[:scheme] ||= :primary

Primer::ButtonComponent.new(**system_arguments) # rubocop:disable Primer/ComponentNameMigration
Primer::ButtonComponent.new(**system_arguments)
}

# Optional secondary action
Expand Down
43 changes: 2 additions & 41 deletions app/components/primer/image.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
# frozen_string_literal: true

module Primer
# Use `Image` to render images.
#
# @accessibility
# Always provide a meaningful `alt`.
class Image < Primer::Component
# @example Default
#
# <%= render(Primer::Image.new(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub")) %>
#
# @example Helper
#
# <%= primer_image(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub") %>
#
# @example Lazy loading
#
# <%= render(Primer::Image.new(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub", lazy: true)) %>
#
# @example Custom size
#
# <%= render(Primer::Image.new(src: Primer::ExampleImage::BASE64_SRC, alt: "GitHub", height: 100, width: 100)) %>
#
# @param src [String] The source url of the image.
# @param alt [String] Specifies an alternate text for the image.
# @param lazy [Boolean] Whether or not to lazily load the image.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(src:, alt:, lazy: false, **system_arguments)
@system_arguments = deny_tag_argument(**system_arguments)

@src = src
@system_arguments[:tag] = :img
@system_arguments[:alt] = alt

return unless lazy

@system_arguments[:loading] = :lazy
@system_arguments[:decoding] = :async
end

def call
render(Primer::BaseComponent.new(src: image_path(@src), **@system_arguments))
end
class Image < Primer::Alpha::Image
status :deprecated
end
end
2 changes: 1 addition & 1 deletion app/lib/primer/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ViewHelperNotFound < StandardError; end
octicon: "Primer::OcticonComponent",
heading: "Primer::Beta::Heading",
time_ago: "Primer::TimeAgoComponent",
image: "Primer::Image"
image: "Primer::Alpha::Image"
}.freeze

HELPERS.each do |name, component|
Expand Down
2 changes: 1 addition & 1 deletion docs/src/@primer/gatsby-theme-doctocat/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
- title: IconButton
url: "/components/beta/iconbutton"
- title: Image
url: "/components/image"
url: "/components/alpha/image"
- title: ImageCrop
url: "/components/imagecrop"
- title: Label
Expand Down
1 change: 1 addition & 0 deletions lib/primer/deprecations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Primer
module Deprecations
# If there is no alternative to suggest, set the value to nil
DEPRECATED_COMPONENTS = {
"Primer::Image" => "Primer::Alpha::Image",
"Primer::Alpha::AutoComplete" => "Primer::Beta::AutoComplete",
"Primer::Alpha::AutoComplete::Item" => "Primer::Beta::AutoComplete::Item",
"Primer::BlankslateComponent" => "Primer::Beta::Blankslate",
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/docs.rake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace :docs do
Primer::Beta::Button,
Primer::Alpha::Layout,
Primer::HellipButton,
Primer::Image,
Primer::Alpha::Image,
Primer::LocalTime,
Primer::OcticonSymbolsComponent,
Primer::ImageCrop,
Expand Down
64 changes: 32 additions & 32 deletions static/arguments.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,38 @@
}
]
},
{
"component": "Image",
"status": "alpha",
"source": "https://github.com/primer/view_components/tree/main/app/components/primer/alpha/image.rb",
"lookbook": "https://primer.style/view-components/lookbook/inspect/primer/alpha/image/default/",
"parameters": [
{
"name": "src",
"type": "String",
"default": "N/A",
"description": "The source url of the image."
},
{
"name": "alt",
"type": "String",
"default": "N/A",
"description": "Specifies an alternate text for the image."
},
{
"name": "lazy",
"type": "Boolean",
"default": "`false`",
"description": "Whether or not to lazily load the image."
},
{
"name": "system_arguments",
"type": "Hash",
"default": "N/A",
"description": "[System arguments](/system-arguments)"
}
]
},
{
"component": "Layout",
"status": "alpha",
Expand Down Expand Up @@ -1731,38 +1763,6 @@
}
]
},
{
"component": "Image",
"status": "alpha",
"source": "https://github.com/primer/view_components/tree/main/app/components/primer/image.rb",
"lookbook": "https://primer.style/view-components/lookbook/inspect/primer/image/default/",
"parameters": [
{
"name": "src",
"type": "String",
"default": "N/A",
"description": "The source url of the image."
},
{
"name": "alt",
"type": "String",
"default": "N/A",
"description": "Specifies an alternate text for the image."
},
{
"name": "lazy",
"type": "Boolean",
"default": "`false`",
"description": "Whether or not to lazily load the image."
},
{
"name": "system_arguments",
"type": "Hash",
"default": "N/A",
"description": "[System arguments](/system-arguments)"
}
]
},
{
"component": "ImageCrop",
"status": "alpha",
Expand Down
1 change: 1 addition & 0 deletions static/audited_at.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Primer::Alpha::Dialog::Footer": "",
"Primer::Alpha::Dialog::Header": "",
"Primer::Alpha::HiddenTextExpander": "",
"Primer::Alpha::Image": "",
"Primer::Alpha::Layout": "",
"Primer::Alpha::Layout::Main": "",
"Primer::Alpha::Layout::Sidebar": "",
Expand Down
2 changes: 2 additions & 0 deletions static/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
},
"Primer::Alpha::HiddenTextExpander": {
},
"Primer::Alpha::Image": {
},
"Primer::Alpha::Layout": {
"FIRST_IN_SOURCE_DEFAULT": "sidebar",
"FIRST_IN_SOURCE_OPTIONS": [
Expand Down
3 changes: 2 additions & 1 deletion static/statuses.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Primer::Alpha::Dialog::Footer": "alpha",
"Primer::Alpha::Dialog::Header": "alpha",
"Primer::Alpha::HiddenTextExpander": "alpha",
"Primer::Alpha::Image": "alpha",
"Primer::Alpha::Layout": "alpha",
"Primer::Alpha::Layout::Main": "alpha",
"Primer::Alpha::Layout::Sidebar": "alpha",
Expand Down Expand Up @@ -60,7 +61,7 @@
"Primer::DropdownMenuComponent": "deprecated",
"Primer::HellipButton": "alpha",
"Primer::IconButton": "deprecated",
"Primer::Image": "alpha",
"Primer::Image": "deprecated",
"Primer::ImageCrop": "alpha",
"Primer::LabelComponent": "beta",
"Primer::LayoutComponent": "alpha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

require "components/test_helper"

class PrimerImageTest < Minitest::Test
class PrimerAlphaImageTest < Minitest::Test
include Primer::ComponentTestHelpers

def test_render_without_loading_attribute_by_default
render_inline(Primer::Image.new(src: "https://github.com/github.png", alt: "alt"))
render_inline(Primer::Alpha::Image.new(src: "https://github.com/github.png", alt: "alt"))

assert_selector("img[src='https://github.com/github.png'][alt='alt']")
refute_selector("[loading]")
end

def test_lazy_loading
render_inline(Primer::Image.new(src: "https://github.com/github.png", alt: "alt", lazy: true))
render_inline(Primer::Alpha::Image.new(src: "https://github.com/github.png", alt: "alt", lazy: true))

assert_selector("img[src='https://github.com/github.png'][alt='alt'][loading='lazy'][decoding='async']")
end

def test_restricts_allowed_system_arguments
with_raise_on_invalid_options(true) do
error = assert_raises(ArgumentError) do
render_inline(Primer::Image.new(src: "https://github.com/github.png", alt: "alt", tag: :div))
render_inline(Primer::Alpha::Image.new(src: "https://github.com/github.png", alt: "alt", tag: :div))
end

assert_includes(error.message, "This component has a fixed tag.")
Expand All @@ -30,7 +30,7 @@ def test_restricts_allowed_system_arguments

def test_strips_denied_system_arguments
with_raise_on_invalid_options(false) do
render_inline(Primer::Image.new(src: "https://github.com/github.png", alt: "alt", tag: :div))
render_inline(Primer::Alpha::Image.new(src: "https://github.com/github.png", alt: "alt", tag: :div))
end

refute_selector("div")
Expand Down
3 changes: 2 additions & 1 deletion test/components/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PrimerComponentTest < Minitest::Test
[Primer::Alpha::TabPanels, { label: "label" }],
[Primer::Alpha::TabNav, { label: "label" }],
[Primer::Alpha::UnderlinePanels, { label: "Panel label" }],
[Primer::Image, { src: "https://github.com/github.png", alt: "alt" }],
[Primer::Alpha::Image, { src: "https://github.com/github.png", alt: "alt" }],
[Primer::LocalTime, { datetime: DateTime.parse("2014-06-01T13:05:07Z") }],
[Primer::ImageCrop, { src: "Foo" }],
[Primer::IconButton, { icon: :star, "aria-label": "Label" }],
Expand Down Expand Up @@ -102,6 +102,7 @@ class PrimerComponentTest < Minitest::Test

def test_registered_components
ignored_components = [
"Primer::Image",
"Primer::Alpha::ActionList::Heading",
"Primer::Alpha::ActionList::Item",
"Primer::Alpha::ActionList::Separator",
Expand Down