You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-link.asciidoc 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ---
  2. title: Link
  3. order: 8
  4. layout: page
  5. ---
  6. [[components.link]]
  7. = Link
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/interaction/link"]
  11. endif::web[]
  12. The [classname]#Link# component allows making hyperlinks. References to
  13. locations are represented as resource objects, explained in
  14. <<dummy/../../../framework/application/application-resources#application.resources,"Images
  15. and Other Resources">>. The [classname]#Link# is a regular HTML hyperlink, that
  16. is, an [literal]#++<a href>++# anchor element that is handled natively by the
  17. browser. Unlike when clicking a [classname]#Button#, clicking a
  18. [classname]#Link# does not cause an event on the server-side.
  19. Links to an arbitrary URL can be made by using an [classname]#ExternalResource#
  20. as follows:
  21. [source, java]
  22. ----
  23. // Textual link
  24. Link link = new Link("Click Me!",
  25. new ExternalResource("http://vaadin.com/"));
  26. ----
  27. You can use [methodname]#setIcon()# to make image links as follows:
  28. [source, java]
  29. ----
  30. // Image link
  31. Link iconic = new Link(null,
  32. new ExternalResource("http://vaadin.com/"));
  33. iconic.setIcon(new ThemeResource("img/nicubunu_Chain.png"));
  34. // Image + caption
  35. Link combo = new Link("To appease both literal and visual",
  36. new ExternalResource("http://vaadin.com/"));
  37. combo.setIcon(new ThemeResource("img/nicubunu_Chain.png"));
  38. ----
  39. The resulting links are shown in <<figure.components.link.basic>>. You could add
  40. a " [literal]#++display: block++#" style for the icon element to place the
  41. caption below it.
  42. [[figure.components.link.basic]]
  43. .[classname]#Link# Example
  44. image::img/link.png[width=30%, scaledwidth=70%]
  45. [[components.link.new-window]]
  46. == Opening a New Window
  47. With the simple constructor used in the above example, the resource is opened in
  48. the current window. Using the constructor that takes the target window as a
  49. parameter, or by setting the target window with [methodname]#setTargetName()#,
  50. you can open the resource in another window, such as a popup browser window/tab.
  51. As the target name is an HTML [literal]#++target++# string managed by the
  52. browser, the target can be any window, including windows not managed by the
  53. application itself. You can use the special underscored target names, such as
  54. [literal]#++_blank++# to open the link to a new browser window or tab.
  55. [source, java]
  56. ----
  57. // Hyperlink to a given URL
  58. Link link = new Link("Take me a away to a faraway land",
  59. new ExternalResource("http://vaadin.com/"));
  60. // Open the URL in a new window/tab
  61. link.setTargetName("_blank");
  62. // Indicate visually that it opens in a new window/tab
  63. link.setIcon(new ThemeResource("icons/external-link.png"));
  64. link.addStyleName("icon-after-caption");
  65. ----
  66. [[components.link.pop-up]]
  67. == Opening as a Pop-Up Window
  68. With the [literal]#++_blank++# target, a normal new browser window is opened. If
  69. you wish to open it in a popup window (or tab), you need to give a size for the
  70. window with [methodname]#setTargetWidth()# and [methodname]#setTargetHeight()#.
  71. You can control the window border style with [methodname]#setTargetBorder()#,
  72. which takes any of the defined border styles [parameter]#BorderStyle.DEFAULT#,
  73. [parameter]#BorderStyle.MINIMAL#, and [parameter]#BorderStyle.NONE#. The
  74. exact result depends on the browser.
  75. [source, java]
  76. ----
  77. // Open the URL in a popup
  78. link.setTargetName("_blank");
  79. link.setTargetBorder(BorderStyle.NONE);
  80. link.setTargetHeight(300);
  81. link.setTargetWidth(400);
  82. ----
  83. == Alternatives
  84. In addition to the [classname]#Link# component, Vaadin allows alternative ways to make hyperlinks.
  85. Also, you can make hyperlinks (or any other HTML) in a [classname]#Label# in HTML content mode.
  86. The [classname]#Button# component has a [parameter]#ValoTheme.BUTTON_LINK# style name that makes it look like a hyperlink, while handling clicks in a server-side click listener instead of in the browser.
  87. However, browsers do not generally allow opening new windows after server side round trip, so for such tasks you need to use the [classname]#BrowserWindowOpener# extension described in <<dummy/../../../framework/advanced/advanced-windows#advanced.windows.popup, "Opening Pop-up Windows">>
  88. == CSS Style Rules
  89. [source, css]
  90. ----
  91. .v-link { }
  92. a { }
  93. .v-icon {}
  94. span {}
  95. ----
  96. The overall style for the [classname]#Link# component is [literal]#++v-link++#.
  97. The root element contains the [literal]#++<a href>++# hyperlink anchor. Inside
  98. the anchor are the icon, with [literal]#++v-icon++# style, and the caption in a
  99. text span.
  100. Hyperlink anchors have a number of __pseudo-classes__ that are active at
  101. different times. An unvisited link has [literal]#++a:link++# class and a visited
  102. link [literal]#++a:visited++#. When the mouse pointer hovers over the link, it
  103. will have [literal]#++a:hover++#, and when the mouse button is being pressed over the link, the
  104. [literal]#++a:active++# class. When combining the pseudo-classes in a selector,
  105. please notice that [literal]#++a:hover++# must come after an
  106. [literal]#++a:link++# and [literal]#++a:visited++#, and [literal]#++a:active++#
  107. after the [literal]#++a:hover++#.
  108. === Icon Position
  109. Normally, the link icon is before the caption.
  110. You can have it right of the caption by reversing the text direction in the containing element.
  111. [source, css]
  112. ----
  113. /* Position icon right of the link caption. */
  114. .icon-after-caption {
  115. direction: rtl;
  116. }
  117. /* Add some padding around the icon. */
  118. .icon-after-caption .v-icon {
  119. padding: 0 3px;
  120. }
  121. ----
  122. The resulting link is shown in <<figure.components.link.new-window>>.
  123. [[figure.components.link.new-window]]
  124. .Link That Opens a New Window
  125. image::img/link-new.png[width=25%, scaledwidth=50%]